2019-09-29 ios13适配

ios13禁用了大部分的kvc,所以如UISearchBar的自定义外观也需要适配

- (void)layoutSubviews {
    [super layoutSubviews];
    //mini模式,不显示背景
    self.searchBarStyle = UISearchBarStyleMinimal;
    //去除背景色
    self.backgroundColor = [UIColor clearColor];
    UITextField *field;
   //获取搜索框中的UITextField控件,需要适配
    if (@available(iOS 13.0 , *)) {
        field =  self.searchTextField;
    }else{
        for (UIView *view in [self.subviews lastObject].subviews) {
            if ([view isKindOfClass:[UITextField class]]) {
                field = (UITextField *)view;
            }
        }
    }
    
    // 重设textField的frame
    field.frame = CGRectMake(0, 4, self.frame.size.width, 36);
    // 设置背景色
    field.borderStyle = UITextBorderStyleNone;
    field.layer.cornerRadius = 18;
    field.layer.masksToBounds = YES;
    field.backgroundColor = [FYColorTool colorFromHexRGB:@"#F3F3F3" alpha:1];
    [field setTintColor:[FYColorTool colorFromHexRGB:@"#F3F3F3" alpha:1]];
    field.textColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:1];
    field.leftView.frame = CGRectMake(0, 0, 25, 25);
    field.leftView.contentMode = UIViewContentModeCenter;
    //改变默认placeholder样式需要适配,不能使用kvc
    if (@available(iOS 13.0 , *)) {
        NSMutableAttributedString *arrStr = [[NSMutableAttributedString alloc]initWithString:field.placeholder attributes:
                                             @{
                                                 NSForegroundColorAttributeName : [UIColor colorWithRed:156/255.0 green:156/255.0 blue:156/255.0 alpha:1],
                                                 NSFontAttributeName:[UIFont systemFontOfSize:placeHolderFont]
                                             }];
        field.attributedPlaceholder = arrStr;
    }else{
        // 设置占位文字字体颜色
        [field setValue:[UIColor colorWithRed:156/255.0 green:156/255.0 blue:156/255.0 alpha:1] forKeyPath:@"_placeholderLabel.textColor"];
        [field setValue:[UIFont systemFontOfSize:placeHolderFont] forKeyPath:@"_placeholderLabel.font"];
    }
//适配ios11的搜索图标在中间的问题
    if (@available(iOS 11.0, *)) {
        // 先默认居中placeholder
        //                [self setPositionAdjustment:UIOffsetMake((field.frame.size.width-self.placeholderWidth)/2, 0) forSearchBarIcon:UISearchBarIconSearch];
        [self setPositionAdjustment:UIOffsetMake(0, 0) forSearchBarIcon:UISearchBarIconSearch];
    }
}

UISegmentedControl也做了变化,setTintColor,什么事都不干了。

 UISegmentedControl *seg = [[UISegmentedControl alloc]initWithItems:@[@"推荐" ,@"关注"]];
    seg.selectedSegmentIndex = 0;
    _selectedIndex = 0;
    self.seg = seg;
    if(@available(iOS 13.0 , *)){
        UIImage *tintImage = [self imageWithColor:[UIColor clearColor] andSize:seg.frame.size];
        UIImage *dividerImage = [self imageWithColor:[UIColor clearColor] andSize:CGSizeZero];
        
        [seg setBackgroundImage:tintImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
        [seg setBackgroundImage:tintImage forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
        [seg setBackgroundImage:tintImage forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
        
        [seg setDividerImage:dividerImage forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
        [seg setDividerImage:dividerImage forLeftSegmentState:UIControlStateSelected rightSegmentState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
        
    }else{
        [seg setTintColor:[UIColor clearColor]];
    }
    [seg setTitleTextAttributes:@{
                                  NSForegroundColorAttributeName : [FYColorTool colorFromHexRGB:@"#333333" alpha:1],
                                  NSFontAttributeName : [UIFont systemFontOfSize:22 weight:UIFontWeightSemibold],
                                  NSBaselineOffsetAttributeName : @2
                                  } forState:UIControlStateSelected];
    [seg setTitleTextAttributes:@{
                                  NSForegroundColorAttributeName : [FYColorTool colorFromHexRGB:@"#333333" alpha:1],
                                  NSFontAttributeName : [UIFont systemFontOfSize:18]
                                  } forState:UIControlStateNormal];
    seg.frame = CGRectMake(0,0, 120, 44);//x、y可以设为0,0 效果一样

    self.navigationItem.titleView = seg;
    seg.apportionsSegmentWidthsByContent = YES;
    [seg addTarget:self action:@selector(homePageSelected:) forControlEvents:UIControlEventValueChanged];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容