| 在项目中我们有的时候需要设置全局导航栏,也有自定义的,下面我们就来看看这些问题 
 
 导航栏遮挡的问题导航栏背景设置和分割线设置如何设置全局返回按钮如何控制导航栏的高度
 导航栏遮挡的问题
 iOS7后默认情况下,导航栏会遮挡ViewController的内容。这个原因是由于导航栏的半透明translucent属性造成的。(想要有半
 透明效果,那导航栏后边要有东西啊,要不然怎么看出来半透明效果)。知道原因那我们就知道该怎么修改了。将导航栏的
 translucent属性设为NO就可以了。
 
 
 [C#] 纯文本查看 复制代码 [[UINavigationBar appearance] setTranslucent:NO];
 除了这个方法还有一个方法可以解决这个问题
 
 从UIViewController的接口文档中,能看到这些API:
 
 
 [C#] 纯文本查看 复制代码 @property(nonatomic,assign) UIRectEdge edgesForExtendedLayout 
NS_AVAILABLE_IOS(7_0); // Defaults to UIRectEdgeAll
@property(nonatomic,assign) BOOL extendedLayoutIncludesOpaqueBars NS_AVAILABLE_IOS(7_0); // Defaults to NO, but bars 
are translucent by default on 7_0. 
@property(nonatomic,assign) BOOL automaticallyAdjustsScrollViewInsets NS_AVAILABLE_IOS(7_0); // Defaults to YES
 于是可以这样:
 
 我们将edgesForExtendedLayout的值设置为UIRectEdgeNone的时候,控制器视图的顶端会向下移动到导航栏的底部。这个属性和导
 航的translucent属性是没有关系的。这个属性只对导航控制器有效,对容器控制器无效。
 extendedLayoutIncludesOpaqueBars从属性名称中我们可以看出这个属性是用来控制布局时是否包含透明Bar。这个属性是和NavBar
 和Tabbar的translucent属性是有关系的。这个属性只有当他们的translucent属性为NO时,这个属性才有⽤。当
 extendedLayoutIncludesOpaqueBars = YES时。布局是从屏幕顶端(底部)开始的,会被导航栏(或TabBar)遮挡。当
 extendedLayoutIncludesOpaqueBars = NO时,布局是从导航栏底部(或TabBar顶部)开始的,不会被导航栏(或TabBar)遮挡。
 automaticallyAdjustsScrollViewInsets属性是用来控制容器视图控制器中的⼦控制器的可滚动区域插值的。例如当导航栏中的某个控制
 器视图是⼀个全屏UITableView,则会在UITableView的上下⾃动插⼊NavBar和Tabbar的⾼度
 
 [C#] 纯文本查看 复制代码 @interface UIViewController (UILayoutSupport)
// These objects may be used as layout items in the NSLayoutConstraint API
@property(nonatomic,readonly,strong) id<UILayoutSupport> topLayoutGuide NS_AVAILABLE_IOS(7_0);
@property(nonatomic,readonly,strong) id<UILayoutSupport> bottomLayoutGuide NS_AVAILABLE_IOS(7_0);
@end
 从文档中看出这个是用来做布局的,官方还给了个例子:
 
 
 [C#] 纯文本查看 复制代码 [button setTranslatesAutoresizingMaskIntoConstraints: NO];
id topGuide = myViewController.topLayoutGuide;
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings (button, topGuide);
[myViewController.view addConstraints:
 [NSLayoutConstraint constraintsWithVisualFormat: @"V:[topGuide]-20-[button]"
 options: 0
 metrics: nil
 views: viewsDictionary]];
[self.view layoutSubviews]; // You must call this method here or the system raises an exception
 这两个属性是实现了<UILayoutSupport>协议的。从这个协议中我们可以得到length,也就是高度。这两个属性和NavBar和
 Tabbar的translucent属性是有关系的。只有当他们的translucent = YES时,他们的length才会返回对应的高度。否则length = 0。
 导航栏背景设置和分割线设置
 
 可以通过barTintColor来设置NavBar的颜色。
 
 
 [C#] 纯文本查看 复制代码 @property(nullable, nonatomic,strong) UIColor *barTintColor NS_AVAILABLE_IOS(7_0) 
UI_APPEARANCE_SELECTOR; // default is nil可以通过背景图片来设置。
 - (void)setBackgroundImage:(nullable UIImage *)backgroundImage forBarMetrics:(UIBarMetrics)barMetrics
 NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
 
 隐藏分割线(阴影线)
 
 
 [C#] 纯文本查看 复制代码 @property(nullable, nonatomic,strong) UIImage *shadowImage NS_AVAILABLE_IOS(6_0) 
UI_APPEARANCE_SELECTOR;通过将shadowImage属性设为⼀个空的UIImage来实现隐藏分割线的效果。前提是导航栏的背景是图片来设置的。当然还有其
 他方法,比如通过遍历NavBar的视图来找到这个分割线,然后删除或隐藏。
 
 如何设置全局返回按钮
 设置返回按钮的图
 ⽚
 
 [C#] 纯文本查看 复制代码 [[UINavigationBar appearance] setBackIndicatorImage:baceImage]; [[UINavigationBar 
appearance] setBackIndicatorTransitionMaskImage:baceImage];
 设置返回按钮的文字
 ⽅方法1:继承UINavigationController
 
 [C#] 纯文本查看 复制代码 //重写⽅法 - (void)pushViewController:(UIViewController *)viewController animated:
(BOOL)animated{
 [super pushViewController:viewController animated:animated];
 viewController.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"" style:(UIBarButtonItemStylePlain) 
target:nil action:nil];
 }
 ⽅方法2⼆:实现<UINavigationControllerDelegate>协议
 
 [C#] 纯文本查看 复制代码 - (void)navigationController(UINavigationController *)navigationController willShowViewController:
(UIViewController *)viewController animated:(BOOL)animated{
 viewController.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:(UIBarButtonItemStylePlain) 
target:nil action:nil];
}
 如何控制导航栏的高度
 
 添加UINavigationBar的Category重载sizeThatFits⽅法或者继承UINavigationBar重写sizeThatFits属性。
 
 [C#] 纯文本查看 复制代码 - (CGSize)sizeThatFits(CGSize)size{
 return (CGSize){
 [UIScreen mainScreen].bounds.size.width,70
 };
}
 
 
 
 
 
 |