iOS15iOS开发iOS常用

Xcode13适配iOS15

2021-10-13  本文已影响0人  Mr姜饼

1.Navigationbar

在iOS15中,UINavigationBar默认是透明的,首次显示的时候会以边缘外观样式的形式展示

ios14 ios15 navigationbar对比如下图


image

scrollEdgeAppearance 适配方式:

///Describes the appearance attributes for the navigation bar to use when an associated UIScrollView has reached the edge abutting the bar (the top edge for the navigation bar). If not set, a modified standardAppearance will be used instead.
@property (nonatomic, readwrite, copy, nullable) UINavigationBarAppearance *scrollEdgeAppearance UI_APPEARANCE_SELECTORAPI_AVAILABLE(ios(13.0));
- (void)newNavBarConfig{
    
    UINavigationBar* navigationBar = self.navigationController.navigationBar;

    UINavigationBarAppearance* barApp = [[UINavigationBarAppearance alloc] init];
    //字体的样式
    barApp.titleTextAttributes = @{
        NSFontAttributeName: [UIFont systemFontOfSize:30],
        NSForegroundColorAttributeName: [UIColor blueColor]
    };
    //改变返回按钮的样式
    UIBarButtonItemAppearance* backAppearance = [[UIBarButtonItemAppearance alloc] initWithStyle:UIBarButtonItemStylePlain];
    UIBarButtonItemStateAppearance* normarl = backAppearance.normal;
    normarl.titleTextAttributes = @{
        NSFontAttributeName: [UIFont systemFontOfSize:30],
        NSForegroundColorAttributeName: [UIColor redColor]
    };
    barApp.backButtonAppearance = backAppearance;
    [barApp setBackIndicatorImage:[UIImage imageNamed:@"search@2x"] transitionMaskImage:[UIImage imageNamed:@"search@2x"]];
    //Done按钮的样式
    UIBarButtonItemAppearance* doneppearance = [[UIBarButtonItemAppearance alloc] initWithStyle:UIBarButtonItemStyleDone];
    UIBarButtonItemStateAppearance* normarl1 = doneppearance.normal;
    normarl1.titleTextAttributes = @{
        NSFontAttributeName: [UIFont systemFontOfSize:20],
        NSForegroundColorAttributeName: [UIColor blackColor]
    };
    barApp.doneButtonAppearance = doneppearance;
    //设置背景图片
    barApp.backgroundImageContentMode = UIViewContentModeScaleAspectFit;
    barApp.backgroundImage = [UIImage imageNamed:@"背景"];
    //常规样式
    navigationBar.standardAppearance = barApp;
    
    //边缘外观样式
    barApp.shadowColor = [UIColor clearColor];
    barApp.shadowImage = [UIImage new];
    barApp.backgroundImage = nil;
    barApp.backgroundColor = [UIColor greenColor];
    navigationBar.scrollEdgeAppearance = barApp;
}


适配方案:

在navvbar首次显示的时候,将以前的样式配置,统一更换成UINavigationBarAppearance配置。并且赋值给standardAppearance普通外观样式 , 且让scrollEdgeAppearance = standardAppearance。

2.Tabbar

tabbar的问题和navigationBar的问题属于同一类,tabbar背景颜色设置失效,字体设置失效,阴影设置失效问题.
同时也新增scrollEdgeAppearance属性,当关联的scrollview滑动到最底部的时候,tabbar的scrollEdgeAppearance边缘外观属性被展示出来。

- (void)setNewBarConfig{
    UITabBarAppearance* appBar = [[UITabBarAppearance alloc] init];
    appBar.backgroundColor = [UIColor redColor];
    appBar.stackedLayoutAppearance.normal.titleTextAttributes = @{
        NSFontAttributeName: [UIFont systemFontOfSize:20],
        NSForegroundColorAttributeName: [UIColor whiteColor]
   };
    appBar.stackedLayoutAppearance.selected.titleTextAttributes = @{
        NSFontAttributeName: [UIFont systemFontOfSize:30],
        NSForegroundColorAttributeName: [UIColor purpleColor]
   };
//    appBar.backgroundImage = [UIImage imageNamed:@"背景"];//依然有效
    self.tabBar.scrollEdgeAppearance = appBar;
    appBar.backgroundColor = [UIColor greenColor];
    appBar.shadowColor = [UIColor clearColor];
    self.tabBar.standardAppearance = appBar;
}

适配方案:

在tabbar首次显示的时候,将以前的样式配置,统一更换成UITabBarAppearance配置。并且赋值给standardAppearance普通外观样式 。

3.sheetPresentationController

通过它可以控制 Modal 出来的 UIViewController 的显示大小,且可以通过拖拽手势在不同大小之间进行切换。只需要在跳转的目标
large 模式 medium模式


image
- (void)enterNextPage3{
    Test2ViewController* VC = [Test2ViewController new];
    UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:VC];
    UISheetPresentationController* sheet =  nav.sheetPresentationController ;
    sheet.detents = @[[UISheetPresentationControllerDetent largeDetent],[UISheetPresentationControllerDetent mediumDetent]];//同时存在large模式和medium模式
    sheet.prefersGrabberVisible = YES;//顶部出现一个抓取器
    [self presentViewController:nav animated:YES completion:nil];
}


4.UIButtonConfiguration

UIButton支持更多配置。UIButton.Configuration是一个新的结构体,它指定按钮及其内容的外观和行为。它有许多与按钮外观和内容相关的属性,如cornerStyle、baseForegroundColor、baseBackgroundColor、buttonSize、title、image、subtitle、titlePadding、imagePadding、contentInsets、imagePlacement等。

- (void)newBtn{
    
    NSMutableAttributedString *attributedTitle = [[NSMutableAttributedString alloc] initWithString:@"登录"];
    [attributedTitle setAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:30],NSForegroundColorAttributeName:[UIColor yellowColor]} range:NSMakeRange(0, 1)];
    [attributedTitle setAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15],NSForegroundColorAttributeName:[UIColor blueColor]} range:NSMakeRange(1, 1)];

    UIButtonConfiguration *  configuration = [UIButtonConfiguration tintedButtonConfiguration];
//    configuration.attributedTitle = attributedTitle;
    configuration.title = @"登录";
    configuration.subtitle = @"请输入账号";
    configuration.image = [UIImage imageNamed:@"search"];
    configuration.imagePadding = 30;//
    
    
    configuration.cornerStyle = UIButtonConfigurationCornerStyleLarge;
    //二段文字的间距
    configuration.titlePadding = 20;
    //图片与文本的间距
    configuration.imagePadding = 20;
    //文本的居中方式
    configuration.titleAlignment = UIButtonConfigurationTitleAlignmentCenter;
    //图片显示的位置(上下左右)
    configuration.imagePlacement =  NSDirectionalRectEdgeTrailing;
    //背景色
    configuration.baseBackgroundColor = [UIColor yellowColor];
    //自带的button大小
//    configuration.buttonSize = UIButtonConfigurationSizeLarge;
    //文字的颜色
    configuration.baseForegroundColor = [UIColor redColor];
    //内容的间距
    configuration.contentInsets = NSDirectionalEdgeInsetsMake(30, 30, 30, 30);

    
    configuration.activityIndicatorColorTransformer = ^UIColor * _Nonnull(UIColor * _Nonnull color) {
        return [UIColor blueColor];
    };
    //
//     configuration.showsActivityIndicator = YES;

    UIButton* btn = [UIButton buttonWithConfiguration:configuration primaryAction:nil];
    [self.view addSubview:btn];
    [btn addTarget:self action:@selector(update:) forControlEvents:UIControlEventTouchUpInside];
    //样式、状态变化回调(选中、未选中、高亮......)
    btn.configurationUpdateHandler = ^(UIButton* btn){
        
        //或者处理其他的逻辑(比如当用户名和密码都同时存在的时候,即可改变状态)
        
        UIButtonConfiguration* config =  btn.configuration;
        if(config.showsActivityIndicator){
            config.title = @"正在加载中...";
        }else{
            config.title = @"登录";
        }
        btn.configuration = config;

    };
//当设置了config之后,下面的这些已经无效了
//    btn.titleLabel.textAlignment = NSTextAlignmentRight;
//    [btn.titleLabel setFont:[UIFont boldSystemFontOfSize:40]];
//    btn.subtitleLabel.font = [UIFont systemFontOfSize:5];
//    [btn.subtitleLabel setTextColor:[UIColor blackColor]];
    [btn sizeToFit];
    btn.center = CGPointMake(self.view.frame.size.width/4, self.view.center.y + 50);
    self.btn = btn;
//    btn.layer.cornerRadius = btn.frame.size.height/2;
//    btn.layer.masksToBounds = YES;
}

- (void)update:(UIButton*)btn{
    UIButtonConfiguration * con =  btn.configuration;
    con.showsActivityIndicator = !btn.configuration.showsActivityIndicator;
    btn.configuration = con;
}
image

5.UITableView

从 iOS 15 开始,TableView 增加sectionHeaderTopPadding属性,默认情况sectionHeaderTopPadding会有22个像素的高度,及默认情况,TableView section header增加22像素的高度

 self.tableView.sectionHeaderTopPadding = 0 

6.CLLocationButton

点击实现定位授权

- (void)locationBtn{
    CLLocationButton* btn = [[CLLocationButton alloc] init];
    btn.label = CLLocationButtonLabelCurrentLocation;
    btn.fontSize = 20;
    btn.icon = CLLocationButtonIconArrowFilled;
    btn.tintColor = [UIColor systemPinkColor];
    btn.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:btn];
    [btn sizeToFit];
    btn.userInteractionEnabled = YES;
    btn.layer.transform = CATransform3DMakeTranslation(100, 200, 0);

}


}

7.UIImage

开启线程执行裁剪功能,执行完毕之后要回到主线程去操作

    [image prepareThumbnailOfSize:CGSizeMake(50, 100) completionHandler:^(UIImage * _Nullable image) {
        //此处开启一个新线程进行
        NSLog(@"裁剪完毕%@",[NSThread currentThread]);
        dispatch_async(dispatch_get_main_queue(), ^{//回归到主线程
            [self afterImage:image];
        });
    }];


8.额外补充: cell.configurationUpdateHandler'

tableviewCell的样式配置UIListContentConfiguration、UIBackgroundConfiguration

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"cellId"];
    
    UIListContentConfiguration * config = cell.defaultContentConfiguration ;
    UIBackgroundConfiguration * config2 = [UIBackgroundConfiguration listPlainCellConfiguration];
    config2.cornerRadius = 10;
    config2.backgroundColor = [UIColor purpleColor];
    config.text = [NSString stringWithFormat:@"样式内容%ld",indexPath.row];
    config.image = [UIImage imageNamed:@"search"];
    cell.contentConfiguration = config;//内容样式
    cell.backgroundConfiguration = config2;//背景样式

    cell.configurationUpdateHandler = ^(__kindof UITableViewCell * _Nonnull cell, UICellConfigurationState * _Nonnull state) {
        UIBackgroundConfiguration* confi = cell.backgroundConfiguration;
        NSLog(@"触发state.isSelected = %d,state.isSwiped = %d,state.isExpanded = %d,state.isHighlighted = %d,state.isFocused = %d,state.isReordering = %d state.isEditing = %d", state.isSelected,state.isSwiped,state.isExpanded,state.isHighlighted,state.isFocused,state.isReordering,state.isEditing);
        if(state.isSelected){//可以用来做”已读置灰的功能需求“,未必就是变颜色,或者是其他的交互
            confi.backgroundColor = [UIColor lightGrayColor];
        }else{
            confi.backgroundColor = [UIColor whiteColor];
        }
        if(state.isHighlighted == NO){//当不在高亮的时候 取消掉选中状态
            cell.selected = NO;
        }
        cell.backgroundConfiguration = confi;
    };
    cell.automaticallyUpdatesContentConfiguration = YES;
    cell.textLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row];//优先级小于config
    return cell;
}


上一篇下一篇

猜你喜欢

热点阅读