美文网首页
2019-09-29 ios13适配

2019-09-29 ios13适配

作者: zxh123456 | 来源:发表于2019-09-29 09:24 被阅读0次

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];

相关文章

  • iOS13 适配问题 看这一篇就够了

    技术参考: apple login IOS13适配-详细 iOS 13 适配(持续更新中) iOS13适配 掘金 ...

  • iOS 13适配

    技术参考: apple login IOS13适配-详细 iOS 13 适配(持续更新中) iOS13适配 掘金 ...

  • 2019-09-29 ios13适配

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

  • 暗黑模式开发

    iOS13暗黑模式适配(项目开发版) iOS 13 DarkMode 暗黑模式 IOS 暗黑模式适配---基础适配

  • iOS13适配更新总结

    前言: iOS13的API的变动和适配问题,我从新特性适配、API 适配、方法弃用、工程适配、SDK 适配、其他问...

  • iOS13适配研究

    iOS13今年秋季会发布,最近深入研究了下公司APP适配iOS13的注意点,适配如下。 1.由于Xcode10移除...

  • iOS13适配

    参考: iOS13 适配踩坑 - 持续更新 iOS 13 适配要点总结 iOS 13 适配要点总结 1、prese...

  • iOS13适配(更新中)

    对于iOS13适配汇总以及遇到的问题注意:以下适配内容,必须适配的会以"必须"标出 1. Dark Model(必...

  • 关于WRNavigationBar iOS12、iOS13导航栏

    集成WRNavigationBar 适配iOS12 iOS13导航栏问题 在修复iOS13下在iPhone11机型...

  • 关于WRNavigationBar iOS12、iOS13导航栏

    集成WRNavigationBar 适配iOS12 iOS13导航栏问题 在修复iOS13下在iPhone11机型...

网友评论

      本文标题:2019-09-29 ios13适配

      本文链接:https://www.haomeiwen.com/subject/seoxpctx.html