iOS 13踩坑: view frame改变无效

2019-10-12  本文已影响0人  某非著名程序员

问题:xib约束view的高度,拖到代码中,然后直接改变view.frame.size.height,在iOS13中无效,在iOS13以前是正常的。

@interface ChangeFrameInValidViewController ()

@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bgViewHeight;
@property (weak, nonatomic) IBOutlet UITextView *textView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *textViewHeight;

@end

@implementation ChangeFrameInValidViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.bgViewHeight.constant = 10;
    self.textViewHeight.constant = 10;
}

- (IBAction)changeFrameAction:(id)sender {
    self.textView.height += 20;
    self.bgView.height += 20;
}

重写了frame方法,发现并非无效,而是修改之后,又会把frame高度等于约束初始化的高度。

从autolayout原理来看,最终布局都会转换成frame,而button事件必定在布局完成后,从逻辑上看是没有问题,而且在iOS13以前都是没有问题。

解决方法:如果使用了约束,就统一用约束修改。如果使用Frame就统一使用frame修改。

上一篇 下一篇

猜你喜欢

热点阅读