iOS13 适配总结

1、UISegmentedControl 修改选择背景色

       if (@available(iOS 13.0, *)) {
            _segmented.selectedSegmentTintColor = Stock_Red;
        } else {
            _segmented.tintColor = Stock_Red;
        }
// 以下代码可以代替上面:
   [_segmented setBackgroundImage:[UIImage imageWithColor:[UIColor clearColor]] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
   [_segmented setBackgroundImage:[UIImage imageWithColor:Stock_Red] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];

// [UIImage imageWithColor:xx]
// 这是我自己封装的方法:返回一张纯色图片 
+ (UIImage *)imageWithColor:(UIColor *)color {
    CGRect rect = CGRectMake(0, 0, 1, 1);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}



2、模态视图样式

代码块语法遵循标准markdown代码,例如:

要改回原来模态视图样式,我们只需要把UIModalPresentationStyle设置为UIModalPresentationFullScreen即可。

ViewController *vc = [[ViewController alloc] init];
vc.title = @"presentVC";
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
nav.modalPresentationStyle = UIModalPresentationFullScreen;
[self.window.rootViewController presentViewController:nav animated:YES completion:nil];



3、私有KVC

在使用iOS 13运行项目时突然APP就crash掉了。定位到的问题是在设置UITextField的Placeholder也就是占位文本的颜色和字体时使用了KVC的方法:

[_textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
[_textField setValue:[UIFont systemFontOfSize:14] forKeyPath:@"_placeholderLabel.font"];

可以将其替换为:

_textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"文本" attributes:@{
NSFontAttributeName:[UIFont systemFontOfSize:16],
NSForegroundColorAttributeName:[UIColor redColor]}];

并且只需要在初始化的时候设置attributedPlaceholder即富文本的占位文本,再重新赋值依然使用placeolder直接设置文本内容,样式不会改变。



4、黑暗模式

取消黑暗模式
在 plist 文件中添加一下键值对:
UIUserInterfaceStyle Light
在这里插入图片描述



5、iOS13 屏幕旋转

这里 ViewController 简称:VC
iOS13以前的 API (在iOS13中失效了),以前在进入ViewController 时可以自动调用,现在不调用了

@interface xxViewController

@end

@implementation xxViewController

// 原需求:屏幕锁定,不支持屏幕旋转,手动让VC横屏    
#pragma mark - Orientation是否支持转屏
- (BOOL)shouldAutorotate{
    return NO;
}
#pragma mark - 支持哪些转屏方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    if (self.isBack) {
        return UIInterfaceOrientationMaskPortrait;
    }else {
        return UIInterfaceOrientationMaskLandscapeRight;
    }
}
#pragma mark - 默认的屏幕方向
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return UIInterfaceOrientationLandscapeRight;
}
@end

iOS 13 中的解决方案

//-----------------------"  AppDelegate  "-----------------------
#import 
#import 
+ (AppDelegate *)shareInstance;
// 旋转屏幕 
-(void)changeOrientation:(UIInterfaceOrientation)toOrientation;
@end


#import "AppDelegate.h"
@interface AppDelegate () 
@end

@implementation AppDelegate

static AppDelegate *_singleInstance;
+(AppDelegate *)shareInstance {
    return _singleInstance;
}
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    if(self.allowRotation) {
        return UIInterfaceOrientationMaskLandscape;
    } else {
        return UIInterfaceOrientationMaskPortrait;
    }
}
//要解决很简单,只需要在方法函数头部添加几句不会被编译器优化掉的代码即可。
#pragma mark - 旋转屏幕
-(void)changeOrientation:(UIInterfaceOrientation)toOrientation {
    NSLog(@"---旋转屏幕= %ld",(long)toOrientation);
  if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
        SEL selector = NSSelectorFromString(@"setOrientation:");
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
        [invocation setSelector:selector];
        [invocation setTarget:[UIDevice currentDevice]];
      
      int val = 0;
      if (toOrientation==UIInterfaceOrientationPortrait) {
          val = UIInterfaceOrientationPortrait;
          
      }else if (toOrientation==UIInterfaceOrientationPortraitUpsideDown){
          val = UIInterfaceOrientationPortraitUpsideDown;
          
      }else if (toOrientation==UIInterfaceOrientationLandscapeLeft){
          val = UIInterfaceOrientationLandscapeRight;
          
      }else if (toOrientation==UIInterfaceOrientationLandscapeRight){
          val = UIInterfaceOrientationLandscapeLeft;
      }
        [invocation setArgument:&val atIndex:2];
        [invocation invoke];
    }
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
 
    //1.AppDelegate
    _singleInstance = self;
    self.application = application;
    return YES;
}
@end

//-----------------------"  xxViewController  "-----------------------
interface xxViewController

@end

@implementation xxViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [AppDelegate shareInstance].allowRotation = YES;
    [[AppDelegate shareInstance] changeOrientation:UIInterfaceOrientationLandscapeLeft];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [AppDelegate shareInstance].allowRotation = NO;
     [[AppDelegate shareInstance] changeOrientation:UIInterfaceOrientationPortrait];
}
@end



6、iOS13 新增编辑手势

复制:三指捏合
剪切:两次三指捏合
粘贴:三指松开
撤销:三指向左划动(或三指双击)
重做:三指向右划动
快捷菜单:三指单击

// 暂时禁用

#ifdef __IPHONE_13_0
- (UIEditingInteractionConfiguration)editingInteractionConfiguration{
    return UIEditingInteractionConfigurationNone;
}
#endif



7、iOS13 新生命周期 SceneDelegate

增加了 #import "SceneDelegate.h"
原来的 #import "AppDelegate.h" 没有了window

要想维持原状,需要在AppDelegate里,添加window,并且删掉Info.plist里的新增键值对
@property (strong, nonatomic) UIWindow *window;

@interface AppDelegate : UIResponder
@property (strong, nonatomic) UIWindow *window;
@end
删掉以下键值对
Application Scene Manifest:2times
Main storyboard file base name:Main

iOS13 适配总结_第1张图片


更新中·····

「文章有不对地方,欢迎批评指正!」

你可能感兴趣的:(iOS)