iOS 13 UIToolbar 自定义报布局错误

忙开发新功能,一直忽略了一些后台警告。今日针对一些警告做一些处理吧!

给键盘加一个自定义UIToolbar,当创建的时候,报错约束冲突了。

上代码

/**
 创建键盘上边完成按钮
 
 @return UIToolbar
 */
-(UIToolbar *)createKeyboardBar {
    if (self.actionBar == nil) {
        self.actionBar = [[UIToolbar alloc] init];
        self.actionBar.translucent = YES;
        [self.actionBar sizeToFit];
        self.actionBar.barStyle = UIBarStyleDefault;
        
        UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"完成"
                                                                       style:UIBarButtonItemStylePlain target:self
                                                                      action:@selector(handleActionBarDone:)];
        UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        [self.actionBar setItems:[NSArray arrayWithObjects:flexible, doneButton, nil]];
        [self.actionBar setTintColor:colorTheme];
    }
    return self.actionBar;
}

懵逼状态,XJBS吧!

1、给doneButton右边也加一个flexibleSpace呢?结果doneButton居中显示了,但依然报错。

2、给doneButton设置Frame呢?[[UIBarButtonItem alloc] initWithCustomView:btn] 左右加flexibleSpace,不报错了,但是居中显示。说明CustomView方向对了。

3、给doneButton左边再加一个占位控件呢?加个View怎么样?

解决

/**
 创建键盘上边完成按钮

 @return UIToolbar
 */
-(UIToolbar *)createKeyboardBar {
    if (self.actionBar == nil) {
        self.actionBar = [[UIToolbar alloc] init];
        self.actionBar.translucent = YES;
        [self.actionBar sizeToFit];
        self.actionBar.barStyle = UIBarStyleDefault;
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 44)];
        [btn setTitle:@"完成" forState:UIControlStateNormal];
        [btn setTitleColor:colorTheme forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(handleActionBarDone:) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithCustomView:btn];
        
        UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-100, 44)];
        UIBarButtonItem *flexible1 = [[UIBarButtonItem alloc] initWithCustomView:view];
        [self.actionBar setItems:[NSArray arrayWithObjects:flexible,flexible1, doneButton,flexible, nil]];
        [self.actionBar setTintColor:colorTheme];
    }
    return self.actionBar;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值