iOS13 適配 夜間模式(深色模式)與其他

http://www.lxweimin.com/p/46b174babe09

iOS13 適配 夜間模式與其他

  • 夜間模式
  • 其他問題:presentViewController
    KVC似有對象

一 :夜間/深色模式 DarkMode

夜間模式是iOS13的重要更新之一,隨之而來的是我們能從系統設置中“顯示與亮度”中選擇“淺色”、“深色”兩種模式,并且可以設置自動切換。(“控制中心”亮度調節中也可直接調節)

已知問題:在系統設置為深色模式時候,無法更改StateBar顏色

  1. 如果不想適配深色模式的兩種方式,任選
    (1).直接在項目的plist文件中設置

參考:https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/iPhoneOSKeys.html#//apple_ref/doc/uid/TP40009252-SW44

    <key>UIUserInterfaceStyle</key>
    <string>Light</string>
(2).在每個UIViewController或者BaseViewController(如果自己有的話),中設置
if (@available(iOS 13.0, *)){
       self.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
    }
  1. 適配深色模式
    首先我們要看一下顯示模式的枚舉值
typedef NS_ENUM(NSInteger, UIUserInterfaceStyle) {
    UIUserInterfaceStyleUnspecified,
    UIUserInterfaceStyleLight,
    UIUserInterfaceStyleDark,
} API_AVAILABLE(tvos(10.0)) API_AVAILABLE(ios(12.0)) API_UNAVAILABLE(watchos);

當前API還沒有提供淺色/深色模式切換時的通知,但是為UIColor添加了新方法:
+ (UIColor *)colorWithDynamicProvider:(UIColor * (^)(UITraitCollection *))dynamicProvider;
該方法通過一個block返回顏色,根據其中UITraitCollection參數,我們可以獲取到當前系統的UIUserInterfaceStyle. 這個方法會在每次系統模式改變后回調,所以我想,我可以在一個顏色中去為當前界面做監聽.

Xcode 11為xcassets帶來更新以自動讀取加載淺色/深色模式的資源,只要修改資源Appearances屬性,來設置是否要支持淺色/深色模式,以及資源內容即可,[UIImage imageNamed:@""]會自動加載淺色/深色資源.


image.png

最后上一段 UIViewController 截圖


vc.JPG

最后最后上一段 UIViewController 代碼

#import "DarkModeViewController.h"

@interface DarkModeViewController ()
{
    UIImageView *_iv2;
    UIUserInterfaceStyle _userInterfaceStyle;
}
@end

@implementation DarkModeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    __weak typeof(self)weakSelf = self;
    UIColor *backgroundColor = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * traitCollection) {
        self->_userInterfaceStyle = traitCollection.userInterfaceStyle;
        [weakSelf performSelector:@selector(traitCollectionChanged:) withObject:traitCollection];
        if (traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
            return [UIColor blueColor];
        }
        return [UIColor yellowColor];
    }];
    self.view.backgroundColor = backgroundColor;
    
    UIImageView *iv = ({
        UIImageView *imageView = [[UIImageView alloc] init];
        [imageView setBackgroundColor:[UIColor clearColor]];
        [imageView setFrame:CGRectMake(20, 100, 100, 100)];
        imageView.image = [UIImage imageNamed:@"icon_star"];
        imageView;
    });
    [self.view addSubview:iv];
    
    _iv2 = ({
        UIImageView *imageView = [[UIImageView alloc] init];
        [imageView setBackgroundColor:[UIColor clearColor]];
        [imageView setFrame:CGRectMake(20, 240, 100, 100)];
        imageView;
    });
    [self.view addSubview:_iv2];
    [self iv2updateImage];
}

- (void)traitCollectionChanged:(UITraitCollection *)traitCollection{
    NSLog(@"traitCollectionChanged:%ld",traitCollection.userInterfaceStyle);
    [self iv2updateImage];
    
}
- (void)iv2updateImage {
    NSLog(@"iv2updateImage:%ld",_userInterfaceStyle);
    if (_userInterfaceStyle == UIUserInterfaceStyleDark) {
        _iv2.image = [UIImage systemImageNamed:@"star.circle.fill"];
    }else{
        _iv2.image = [UIImage systemImageNamed:@"star.circle"];
    }
}
@end

二 :其他問題

  1. presentViewController
    modalPresentationStyle參數有 iOS12 之前的UIModalPresentationFullScreen改為了UIModalPresentationPageSheet,
    在需要presentViewController FullScreen樣式,需要提前設置

2.可能存在問題的私有KVC (textField 的 Placeholder)
放棄使用 KVC 去修改,將修改
_placeholderLabel.textColor 和 _placeholderLabel.font
改為

    UITextField *_textField = [UITextField new];
    
    _textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"UITextField attributedPlaceholder"
                                                                       attributes:@{NSFontAttributeName:_textField.font,
                                                                                    NSForegroundColorAttributeName:[UIColor yellowColor]}];
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。