[英]Set titleview for all navigationcontroller and navigationitem
[[[UINavigationBar appearance] setTitleView:button]
我试图使用上面的代码行将标题视图设置为所有导航项目的按钮,但是它不起作用。如何通过在appdelegate中编写小代码来设置项目中所有导航栏的标题视图?
自定义导航栏的外观
可以修改barStyle,tintColor和半透明属性,但是绝对不能直接更改UIView级别的属性,例如框架,边界,alpha或隐藏的属性。
有关更多详细信息,请遵循apple doc UINavigationBar类参考。
编辑->
我解决了您的查询
[[UINavigationBar appearance] addSubview:yourView];
其他导航相关查询
试试这个家伙...
//put whatever object in this view..for example button that you want to put...
UIView* ctrl = [[UIView alloc] initWithFrame:navController.navigationBar.bounds];
ctrl.backgroundColor = [UIColor yellowColor];
ctrl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[navController.navigationBar addSubview:ctrl];
让我知道它是否有效!!!!
快乐编码!
这是在addDelegate中创建导航控制器并为其设置标题的方式,您必须将以下代码添加到didFinishLaunchingWithOptions方法中。 注意,您需要为viewController设置一个根视图,该根视图将显示在navigationController内部的viewController。
yourViewController *mainVC = [[yourViewController alloc]init];
UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:mainVC];
navigationController1.title = @"title";
[window addSubview:navigationController1.view];
如果您使用iOS4,则可以覆盖drawRect
@implementation UINavigationBar (UINavigationBarCategory)
-(void)drawRect:(CGRect)rect
{
UIImageView *itleImageView = //create object
[self addSubView:titleImageView];
//set title view in navigation bar
}
@end
iOS 5
if ([[UIDevice currentDevice].systemVersion floatValue] >= 5.0) {
if ([self.navigationController.navigationBar respondsToSelector:@selector( setBackgroundImage:forBarMetrics:)]){
UIImageView *itleImageView = //create object
[self.navigationController.navigationBar addSubView:titleImageView];
}
}
我想这是对的,因为我还没有执行。
我想在每个NavigationController中添加徽标,所以我做了UINavigationController
的子类,并在viewDidLoad
-Method中使用了它。
UIImage *logo =[UIImage imageNamed:@"logo"];
CGFloat navWidth = self.navigationBar.frame.size.width;
CGFloat navHeight = self.navigationBar.frame.size.height;
UIImageView *logoView = [[UIImageView alloc] initWithFrame:CGRectMake(navWidth-logo.size.width - 5,
(navHeight - logo.size.height) / 2,
logo.size.width,
logo.size.height)];
logoView.image = logo;
[self.navigationBar addSubview:logoView];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.