針對Xcode 9 + iOS11 的修改,及iPhone X的適配


1,UIScrollView的automaticallyAdjustsScrollViewInsets 失效了。

automaticallyAdjustsScrollViewInsets,當設置為YES時(默認YES),如果視圖里面存在唯一一個UIScrollView或其子類View,那么它會自動設置相應的內邊距,這樣可以讓scroll占據整個視圖,又不會讓導航欄遮蓋。

iOS11這個屬性失效了,表現在App的現象就是TableHeaderView的背景圖原本覆蓋導航欄的,現在在導航欄的下方。

這是因為iOS 11為UIScrollView 添加了新的屬性contentInsetAdjustmentBehavior 這是一個枚舉。

修改代碼如下:

    if (@available(iOS 11.0, *)) {
        self.centerTable.contentInsetAdjustmentBehavior  = UIScrollViewContentInsetAdjustmentNever;
    } else {
       self.automaticallyAdjustsScrollViewInsets = NO;
    }

 

2, 相冊讀取權限

iOS11下,蘋果對相冊的權限key做了調整,原來的 NSPhotoLibraryUsageDescription ,在iOS11之后,改成了NSPhotoLibraryAddUsageDescription。

    <key>NSPhotoLibraryAddUsageDescription</key>  //iOS 11
    <string>App需要您的同意,才能訪問相冊</string>  
    <key>NSPhotoLibraryUsageDescription</key>
    <string>App需要您的同意,才能訪問相冊</string>   //<iOS10

 

3,導航欄

iOS11對導航欄做了比較大的更改;

表現一,以前使用如下方法設置導航欄UIBarButtonItem。到了iOS11上。UIBarButtonItem會被Tint Color渲染,原油顏色被沖掉。

解決方法 : 設置UIImage的渲染模式——UIImage.renderingMode為 UIImageRenderingModeAlwaysOriginal——始終繪制圖片原始狀態,不使用Tint Color。  

  UIImage *backImage = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

//設置導航欄返回按鈕,iOS11上顏色會被沖掉
- (void)setNavigationBarBackButtonItem:(NSString *)image
{
    UIImage *backImage = [UIImage imageNamed:image];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:backImage style:UIBarButtonItemStylePlain target:self action:@selector(popViewControllerAnimated)];
    backItem.tintColor = [UIColor colorWithPatternImage:backImage];
    backItem.title = @"";
    self.navigationItem.leftBarButtonItem = backItem;
}

用  initWithCustomView的方法,賦一個Button上去,不會用這個受上面影響。

    _leftBtn = [[UIButton alloc] ];
    [_leftBtn setImage:image forState:UIControlStateNormal];
    _leftBtn.backgroundColor = [UIColor cyanColor];
    _leftBtn.imageView.contentMode = UIViewContentModeScaleAspectFit;
    _leftBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [_leftBtn addTarget:self action:@selector(leftBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_leftBtn];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;

表現二,iOS11上initWithCustomView的方法給導航欄設置按鈕,如果圖片未加@3x后綴,圖片會顯示超大,超出了原始尺寸。

解決方法就是給圖片加上@3x。

 

 

4,iPhone X適配

iPhone X豎屏時占滿整個屏幕的控制器的view的safeAreaInsets是(44,0,34,0),橫屏是(0,44,21,44),inset后的區域正好是safeAreaLayoutGuide區域

 

1)LaunchImage————添加啟動圖片

{

"extent" : "full-screen",

"idiom" : "iphone",

"subtype" : "2436h",

"filename" : "LaunchImage-1125*2436@3x.jpg", 

"minimum-system-version" : "11.0",

"orientation" : "portrait",

"scale" : "3x"

}

 

 

 2),頂部動態獲取狀態欄和標題欄的高度

//為適配iPhone X,動態獲取狀態欄和標題欄的高度
- (CGFloat)heightAboveSafeArea
{
    //狀態欄
    CGRect statusRect = [[UIApplication sharedApplication] statusBarFrame];
    //標題欄
    CGRect navRect = self.navigationController.navigationBar.frame;
    CGFloat heightOffset = statusRect.size.height + navRect.size.height;
    return heightOffset;
}

 

3),底部按鈕交互在safeArea之外了,需要上移34pt

做法:添加contentView,底部按鈕以contentView 作為依賴即可


//viewdidload中加 WS(ws); [self.contentView mas_remakeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(ws.view).priorityLow(); }]; /** 用來適配iPhone X 底部控件直接以ContentView為依賴 @return vivien add */ - (UIView *)contentView { if(!_contentView){ _contentView = [[UIView alloc]init]; [self.view addSubview:_contentView]; } return _contentView; } // - (void)viewLayoutMarginsDidChange { [super viewLayoutMarginsDidChange]; WS(ws); [self.contentView mas_remakeConstraints:^(MASConstraintMaker *make) { if ([UIDevice currentDevice].systemVersion.floatValue >= 11.0) { make.edges.equalTo(ws.view).insets(ws.view.safeAreaInsets); } else { make.edges.equalTo(ws.view); } }]; }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM