setTranslucent
ios 7 之后,setTranslucent=yes 默认的 则状态栏及导航栏底部为透明的,界面上的组件应该从屏幕顶部开始显示,因为是半透明的,可以看到,所以为了不和状态栏及导航栏重叠,第一个组件的y应该从44+20的位置算起.
如果设置成no,则状态栏及导航样不为透明的,界面上的组件就是紧挨着导航栏显示了,所以就不需要让第一个组件在y方向偏离44+20的高度了.
1.设置navigationBar透明
方法一 (所有界面的都改成了透明)
UIGraphicsBeginImageContext(CGSizeMake(self.view.bounds.size.width, 64));
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
CGContextFillRect(context, CGRectMake(0, 0, self.view.bounds.size.width, 64));
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.clipsToBounds = YES;
方法二 (只是当前页透明)在当前页面实现UINavigationControllerDelegate
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.navigationController.delegate = self;
}
-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
//如果是当前控制器,则隐藏背景;如果不是当前控制器,则显示背景
if (viewController == self) {
for (UIView *view in [self.navigationController.navigationBar subviews]) {
// NSLog(@"%@",NSStringFromClass(view.class));
if ([view isKindOfClass:NSClassFromString(@"_UIBarBackground")]) {
//最好使用隐藏,指不定什么时候你又想让他出现
view.hidden = YES;
//如果不想让它一直出现,那么可以移除
[view removeFromSuperview];
}
}
} else {
for (UIView *view in [self.navigationController.navigationBar subviews]) {
if ([view isKindOfClass:NSClassFromString(@"_UIBarBackground")]) {
view.hidden = NO;
}
}
}
}
2. 修改中间title的颜色color、字体font、阴影shadow
UIColor * color = [UIColor whiteColor];
UIFont *font = [UIFont systemFontOfSize:15];
NSMutableDictionary *dict=[NSMutableDictionary dictionary];
[dict setObject:color forKey:NSForegroundColorAttributeName];
[dict setObject:font forKey:NSFontAttributeName];
[self.navigationController.navigationBar setTitleTextAttributes:dict];
更多见appleAPI词条:Character Attributes
网友评论