因为近来在项目中踩了一些坑,所以今天写这篇文章的目的就当归纳总结一下
translucent
iOS7及iOS7之后由于navigationBar.translucent
默认为YES,原点在(0,0)点,当设置为NO时,原点坐标在(0,64)点屏幕会多一个导航栏的高度,从图层上看的话,控制器相对高度比屏幕高度会低64px。
automaticallyAdjustsScrollViewInsets
当导航栏存在的情况下想让UIScrollView或其子类占据整个视图
if (@available(iOS 11.0, *)) {
self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
} else {
self.automaticallyAdjustsScrollViewInsets = NO;
self.edgesForExtendedLayout = UIExtendedEdgeNone;
}
contentInsetAdjustmentBehavior
iOS11中automaticallyAdjustsScrollViewInsets
方法被废弃,我们需要使用UIScrollView的 contentInsetAdjustmentBehavior
属性来替代它。
contentInsetAdjustmentBehavior
枚举值:
typedef NS_ENUM(NSInteger, UIScrollViewContentInsetAdjustmentBehavior) {
UIScrollViewContentInsetAdjustmentAutomatic, 和scrollableAxes一样,scrollView会自动计算和适应顶部和底部的内边距并且在scrollView 不可滚动时,也会设置内边距.
UIScrollViewContentInsetAdjustmentScrollableAxes, //自动计算内边距.
UIScrollViewContentInsetAdjustmentNever, //不计算内边距,
UIScrollViewContentInsetAdjustmentAlways, //根据safeAreaInsets 计算内边距
} API_AVAILABLE(ios(11.0),tvos(11.0));
self.navigationController.navigationBar.translucent = NO;
相当于self.automaticallyAdjustsScrollViewInsets = NO;self.edgesForExtendedLayout = UIRectEdgeNone;
相当于UIScrollViewContentInsetAdjustmentNever
导航栏透明
if (@available(iOS 13.0, *)) {
[self.navigationController.navigationBar.standardAppearance configureWithTransparentBackground];
self.navigationController.navigationBar.standardAppearance.shadowColor = [UIColor clearColor];
}else{
//导航栏透明
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
//去掉下划线
self.navigationController.navigationBar.shadowImage = [UIImage new];
}
或者
// 设置透明导航栏
UIView *barImageView = self.navigationController.navigationBar.subviews.firstObject;
barImageView.alpha = 0.0;
导航栏颜色
- (void)setBackColor:(UIColor *)backColor{
if (@available(iOS 13.0, *)){
[self.standardAppearance configureWithOpaqueBackground];
[self.standardAppearance setBackgroundColor:backColor];
[self.standardAppearance setBackgroundImage:nil];
}else{
[self setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
[self setBarTintColor:backColor];
}
}
恢复系统默认导航栏
if (@available(iOS 13.0, *)) {
[self.navigationController.navigationBar.standardAppearance configureWithDefaultBackground];
self.navigationController.navigationBar.standardAppearance.shadowImage = nil;
} else {
// Fallback on earlier versions
}
导航栏字体颜色
- (void)setTitleColor:(UIColor *)titleColor font:(UIFont *)font{
//文字颜色
if (@available(iOS 13.0, *)){//这个方法和self.navigationController.navigationBar.barTintColor = [UIColor greenColor];这个方法有冲突,这样设置了字体颜色和大小之后,导航栏的颜色设置就没效果了
[self.navigationController.navigationBar.standardAppearance setTitleTextAttributes:@{NSFontAttributeName:font, NSForegroundColorAttributeName:titleColor}];
}else{
[self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:font , NSForegroundColorAttributeName:titleColor}];
}
}
暂时就这些了吧,后面想到了再补充。
网友评论