网上已经很多适配文章了,这里说一下我们项目遇到、而网文遗漏一些的iOS11、iPhoneX适配问题。
1.缺失 iPhone X 尺寸的启动图,导致应用起来后,顶部和底部有大片黑边。提供一个1125*2436尺寸的启动图即可;
2.导航栏后退按钮样式怪异。下图中前者是iOS11前的效果,后者是iOS11后的。
解决办法:增加ViewController的category并实现以下方法,在进入ViewController 前调用 configureBackBarButtonItemWithNormalImage:hilightedImage:
- (void)configureBackBarButtonItemWithNormalImage:(UIImage *)normalImg hilightedImage:(UIImage *)hlImg
{
// iOS 11.0 前后导航栏的UI结构发生很大变化,对于返回按钮的实现方式也是不同
if (@available(iOS 11.0, *)) {
// iOS 11.0 以后的处理方式
UIImage *backButtonImage = [normalImg?:hlImg imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
self.navigationController.navigationBar.backIndicatorImage = backButtonImage;
self.navigationController.navigationBar.backIndicatorTransitionMaskImage = backButtonImage;
UIBarButtonItem *returnButtonItem = [[UIBarButtonItem alloc] initWithCustomView:[UIView new]];
self.navigationItem.backBarButtonItem = returnButtonItem;
} else {
// iOS 11.0 前处理方式
UIBarButtonItem *returnButtonItem = [[UIBarButtonItem alloc]
initWithTitle:@""
style:UIBarButtonItemStylePlain
target:nil
action:nil];
UIImage *realNormalImg = normalImg?:hlImg;
UIImage *realHighlightedImg = hlImg?:realNormalImg;
if (realNormalImg) {
[returnButtonItem setBackButtonBackgroundImage:[realNormalImg resizableImageWithCapInsets:UIEdgeInsetsMake(0, realNormalImg.size.width, 0, 0)]
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
}
if (realHighlightedImg) {
[returnButtonItem setBackButtonBackgroundImage:[realHighlightedImg resizableImageWithCapInsets:UIEdgeInsetsMake(0, realNormalImg.size.width, 0, 0)]
forState:UIControlStateHighlighted
barMetrics:UIBarMetricsDefault];
}
[returnButtonItem setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault];
self.navigationItem.backBarButtonItem = returnButtonItem;
}
}
3.leftBarButtonItem 距离屏幕左边太宽。
这个问题在iOS 11 以下可以通过设置一个负数 space 的 barItem 来缩减间隙。但是iOS 11 以后失效了。对比网上其他人的方法,我使用另一种方法,主要思路是使用 initWithCustomView: 方法,把操作按钮包装成 UIBarButtonItem,设置UIButton 的contentHorizontalAlignment为Left。但但但这个方法有一个问题就是在 iOS11 leftBarItem 距离左边永远存在16pt的大小,即使你可能通过contentInsets可以把图片往左边靠,但实际上他的frame依然没改变,这个16pt的间隙是无法点击的。
- (UIBarButtonItem *)createBackBarButtonItemWithActionBlock:(void(^)(id sender))actionBlock
{
return [self createLeftBarButtonItemWithIconImage:[[UIImage imageNamed:@"back_gold"] imageByTintColor:[UIColor whiteColor]] itemWidth:30 actionBlock:actionBlock];
}
- (UIBarButtonItem *)createCloseBarButtonItemWithActionBlock:(void(^)(id sender))actionBlock
{
return [self createLeftBarButtonItemWithIconImage:[[UIImage imageNamed:@"close_gold_no_edge"] imageByTintColor:[UIColor whiteColor]] itemWidth:30 actionBlock:actionBlock];
}
- (UIBarButtonItem *)createLeftBarButtonItemWithIconImage:(UIImage*)iconImage
itemWidth:(CGFloat)itemWidth
actionBlock:(void(^)(id sender))actionBlock
{
UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, itemWidth, 30)];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = contentView.bounds;
[button setTitle:nil forState:UIControlStateNormal];
[button setImage:iconImage forState:UIControlStateNormal];
[button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
if (actionBlock) {
[button setBlockForControlEvents:UIControlEventTouchUpInside block:actionBlock];
}
[contentView addSubview:button];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:contentView];
return barButtonItem;
}
UIBarButtonItem *closeItem = [self createCloseBarButtonItemWithActionBlock:^(id sender) {
// TODO something
}];
UIBarButtonItem *backItem = [self createBackBarButtonItemWithActionBlock:^(id sender) {
// TODO something
}];
self.navigationItem.leftBarButtonItems = @[backItem,closeItem];
参考链接:
https://juejin.im/entry/59bf88856fb9a00a5474e394
http://blog.csdn.net/csdnnews/article/details/78127641
http://www.10tiao.com/html/216/201709/2652552758/2.html