我正在爬行运球,并找到了附加的设计。我想知道如何制作这样的自定义导航栏。我的意思是如何创建一次导航栏,并为每个视图控制器隐式地重用它。
我在考虑拥有一种根视图控制器并继承其他视图控制器,但我不知道如何做到这一点。
任何想法或链接都会被提及!
干杯。
西里尔

发布于 2012-02-16 07:11:56
多亏了iOS5,你现在可以自定义UINavigationBar的外观,而不必创建一个子类或创建一个类别。
下面的代码块(将其放在applicationDidFinishLoading:方法中)将整个应用程序的UINavigationBar更改为您提供的任何图像。
请注意,这仅适用于iOS5
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"nav bar.png"] forBarMetrics:UIBarMetricsDefault];
但是,通过在viewDidLoad中使用以下代码,您还可以根据所在的视图控制器更改单个UINavigationBar的外观。
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav bar.png"] forBarMetrics:UIBarMetricsDefault];
上面的代码只是讨论了iOS5带来的定制UINavigationBar外观的新方法。但是,它没有讨论按钮的实现方式。
然而,添加按钮完全是另一回事。为此,我建议将UINavigationBar子类化,然后在需要的地方添加按钮。您甚至可以只使用一个标准的UINavigationBar,而只使用一个自定义的UIBarButtonItem,它运行在特定的视图上。
例如:
UIView *rightButton = [[[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 30.0f, 30.0f)] autorelease];
[rightButton addSubview:[UIImage imageNamed:@"rightButtonImage.png"]];
UIBarButtonItem *rightButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:rightButton] autorelease];
[rightButtonItem setAction:@selector(rightButtonAction:)];我没有测试过该代码,所以它不是一个复制/粘贴解决方案,但它让您了解了需要做些什么才能实现“自定义”外观的UIBarButtonItems。
祝好运!
发布于 2012-02-14 04:11:08
在较早的iOS版本中,您必须将UINavigationBar子类化,即:
@interface CustomNavigationBar : UINavigationBar
@end
@implementation CustomNavigationBar
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
self.opaque = YES;
self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"CustomBackground"]];
}
return self;
}
- (void)drawRect:(CGRect)rect {
// Skip standard bar drawing
}
@end要在视图控制器中使用自定义导航栏,应该在XIB中将标准类名称更改为CustomNavigationBar。
此外,您还可以通过编程方式设置自定义导航栏:
UIViewController *tempController = [[[UIViewController alloc] init] autorelease];
UINavigationController *navigationController = [[[UINavigationController alloc] initWithRootViewController:tempController] autorelease];
NSData *archive = [NSKeyedArchiver archivedDataWithRootObject:navigationController];
NSKeyedUnarchiver *unarchiver = [[[NSKeyedUnarchiver alloc] initForReadingWithData:archive] autorelease];
[unarchiver setClass:[CustomNavigationBar class] forClassName:@"UINavigationBar"];
UINavigationController *customNavigationController = [unarchiver decodeObjectForKey:@"root"];
UIViewController *contentController = [[[ContentViewController alloc] init] autorelease];
customNavigationController.viewControllers = [NSArray arrayWithObject:contentController];现在customNavigationController有了自定义导航栏。
发布于 2011-12-18 09:27:33
为NavigationBar设置自定义背景
UIImage *navBackground =[[UIImage imageNamed:@"navbarBackground"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UINavigationBar appearance] setBackgroundImage:navBackground forBarMetrics:UIBarMetricsDefault];然后在视图控制器中设置left、rightBarButtons和title的自定义视图。
self.navigationItem.titleView = customTitleView;
self.navigationItem.leftBarButtonItem = customBarButton;
self.navigationItem.rightBarButtonItem = customBarButton2;https://stackoverflow.com/questions/8548313
复制相似问题