我有一个带有4个标签的应用程序.每个选项卡都是UINavigationController.4个UINavigationBar选项卡应该看起来相同,具有自定义背景图像,自定义backButton和触发功能的自定义右键.
我想在我的代码中只进行一次自定义,而不是在每个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
Run Code Online (Sandbox Code Playgroud)
但我没有设法自定义后退和右按钮或指定右按钮的操作.
有没有办法在appDelegate中这样做,就像背景图像一样?
或者我应该在每个RootViewController中进行自定义吗?
kan*_*ani 13
在viewWillAppear方法中写下面的代码
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;
Run Code Online (Sandbox Code Playgroud)
并为backButton编写动作事件.
-(IBAction)gotoBack:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
Run Code Online (Sandbox Code Playgroud)