我有一个有 4 个选项卡的应用程序。每个选项卡都是一个 UINavigationController。 4 个 UINavigationBar 选项卡应该看起来相同,具有自定义背景图像、自定义后退按钮和触发功能的自定义右键。
我只想在我的代码中进行一次这些自定义,而不是在每个 RootViewController 中进行一次。
我通过将此代码插入到我的 appDelegate 中来设法获得自定义背景图像:
@implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: @"MyNavigationBar.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end
但我没有设法自定义后退按钮和右侧按钮或指定右侧按钮的操作。
有没有办法在 appDelegate 中做到这一点,就像背景图像一样?
或者我应该在每个 RootViewController 中进行自定义?
将下面的代码写入viewWillAppear
method
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *butImage = [[UIImage imageNamed:@"back.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:10];
[button setBackgroundImage:butImage forState:UIControlStateNormal];
[button addTarget:self action:@selector(gotoBack:) forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake(0, 0, 48, 30);
UIBarButtonItem *backButton = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
self.navigationItem.leftBarButtonItem = backButton;
并编写后退按钮的操作事件。
-(IBAction)gotoBack:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)