ios*_*eak 6 cocoa-touch objective-c
我正在制作一个iPhone应用程序,允许用户返回UINavigationBar.然而,它现在的样子是可怕的.我试图用我自己的图像(减去默认的UIBarButtonItem背景)来自定义它.我的图片包含我的自定义背景,但您仍然可以看到左侧和右侧的部分按钮.
UIBarButtonItem *cancelButton = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back_button_normal.png"] style:UIBarButtonItemStylePlain target:self action:@selector(cancel:)] autorelease];
self.navigationItem.leftBarButtonItem = cancelButton;
Run Code Online (Sandbox Code Playgroud)
有没有办法删除那个空间?有没有可能我可以使用UIButton所以我可以完全自定义它?我在界面构建器中做了一些事情,我将UIButton拖入UINavigationBar的rightButton,它完美地运行.无论如何我可以通过编程方式进行吗?
谢谢!
以下是它的外观:

编辑#1:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(cancel:) forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Show View" forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"back_button_normal.png"] forState:UIControlStateNormal];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
[self.navigationItem setLeftBarButtonItem:barButtonItem];
Run Code Online (Sandbox Code Playgroud)
以下是我为UINavigationBar创建背景的方法(在我的RootViewController中):
@implementation UINavigationBar (UINavigationBarCustomDraw)
- (void) drawRect:(CGRect)rect {
[self setTintColor:[UIColor colorWithRed:0.85f green: 0.85f blue:0.85f alpha:1]];
if ([self.topItem.title length] > 0 && ![self.topItem.title isEqualToString:@"Back to ..."]) {
[[UIImage imageNamed:@"UINavigationBar_background.png"] drawInRect:rect];
CGRect frame = CGRectMake(0, 0, 320, 44);
UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];
[label setBackgroundColor:[UIColor clearColor]];
label.font = [UIFont boldSystemFontOfSize: 20.0];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:1];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.text = self.topItem.title;
self.topItem.titleView = label;
} else {
[[UIImage imageNamed:@"login_button.png"] drawInRect:rect];
self.topItem.titleView = [[[UIView alloc] init] autorelease];
}
}
@end
Run Code Online (Sandbox Code Playgroud)
小智 14
结合两个答案并添加了后退按钮功能
// Custom Navigation Bar Back Button
// Create Button
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0,0,54,32); // width=54, height=32
// Set Button Image
NSString *backButtonURL = [[NSBundle mainBundle]pathForResource:@"back" ofType:@"png"];
UIImage *backButtonImage = [UIImage imageWithContentsOfFile:backButtonURL];
[button setBackgroundImage:backButtonImage forState:UIControlStateNormal];
// Important: Set Button Action to go back
[button addTarget:self.navigationController action:@selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
Run Code Online (Sandbox Code Playgroud)
aka*_*kyy 11
如果要UIButton通过obj-c 添加到导航栏,您的代码应如下所示:
UIButton *button = /* initialize your button */
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
[self.navigationItem setLeftBarButtonItem:barButtonItem];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13875 次 |
| 最近记录: |