Objective-C中点击按钮无法更新UILabel的centerXAnchor问题
问题修复:点击按钮更新UILabel约束不生效的解决办法
嘿,我帮你排查出几个关键问题,咱们一个个来修复:
核心问题分析
你遇到的点击按钮后约束不生效,主要是因为旧约束没有被正确移除,再加上代码里还有几个小错误导致的,具体如下:
1. 旧约束未失效,导致约束冲突
你在点击事件里直接给self.MyLblConstraint赋值了新约束,但原来的旧约束还处于active = YES状态——这时候两个约束(旧的centerX multiplier=0.5,新的multiplier=1)会冲突,系统会自动忽略其中一个,自然看不到更新效果。
2. 错误初始化UILabel
你在.h里声明MyLbl是UILabel,但初始化时写成了self.MyLbl = [UIView new];,这会让它变成普通UIView,根本不是标签,就算约束生效也看不到文本。
3. 按钮添加到错误的视图上
MyCustomView是UIView的子类,它没有self.view这个属性,你把按钮添加到self.view会导致按钮无法显示甚至崩溃,应该添加到self(也就是自定义视图本身)。
修复后的完整代码
MyCustomView.h
#import <UIKit/UIKit.h> @interface MyCustomView : UIView @property (nonatomic, strong) UILabel *myLbl; // 规范命名:小写开头 @property (nonatomic, strong) NSLayoutConstraint *myLblCenterXConstraint; - (instancetype)initWithFrame:(CGRect)frame; // 用instancetype代替id更规范 @end
MyCustomView.m
#import "MyCustomView.h" @implementation MyCustomView @synthesize myLbl; @synthesize myLblCenterXConstraint; - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.translatesAutoresizingMaskIntoConstraints = NO; self.backgroundColor = [UIColor whiteColor]; // 正确初始化UILabel并设置文本、字体 NSString *lblTxtStr = @"This is My Label Text"; UIFont *lblFont = [UIFont fontWithName:@"Helvetica-Bold" size:18]; CGSize lblTxtStrSize = [lblTxtStr sizeWithAttributes:@{NSFontAttributeName: lblFont}]; CGSize lblFrameSize = CGSizeMake(ceilf(lblTxtStrSize.width), ceilf(lblTxtStrSize.height)); self.myLbl = [UILabel new]; self.myLbl.translatesAutoresizingMaskIntoConstraints = NO; self.myLbl.text = lblTxtStr; self.myLbl.font = lblFont; self.myLbl.backgroundColor = [UIColor lightGrayColor]; // 方便看位置 [self addSubview:self.myLbl]; // 设置固定宽高和底部约束 [self.myLbl.widthAnchor constraintEqualToConstant:lblFrameSize.width].active = YES; [self.myLbl.bottomAnchor constraintEqualToAnchor:self.bottomAnchor].active = YES; [self.myLbl.heightAnchor constraintEqualToConstant:lblFrameSize.height].active = YES; // 初始化centerX约束 self.myLblCenterXConstraint = [self.myLbl.centerXAnchor constraintEqualToAnchor:self.centerXAnchor multiplier:0.5]; self.myLblCenterXConstraint.active = YES; // 创建按钮并添加到自定义视图(self) UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; btn.translatesAutoresizingMaskIntoConstraints = NO; [btn setTitle:@"test 1" forState:UIControlStateNormal]; [btn addTarget:self action:@selector(myBtnClick:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:btn]; // 设置按钮约束(注意这里的视图字典要对应) [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[btn]|" options:0 metrics:nil views:@{@"btn": btn}]]; [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[myLbl]-100-[btn(50)]" options:0 metrics:nil views:@{@"myLbl": self.myLbl, @"btn": btn}]]; } return self; } - (void)myBtnClick:(id)sender { NSLog(@"My Button Click"); // 关键:先把旧约束设为失效 self.myLblCenterXConstraint.active = NO; // 创建并激活新约束 self.myLblCenterXConstraint = [self.myLbl.centerXAnchor constraintEqualToAnchor:self.centerXAnchor]; self.myLblCenterXConstraint.active = YES; // 执行动画更新布局 [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:1 initialSpringVelocity:1 options:UIViewAnimationOptionCurveEaseOut animations:^{ [self layoutIfNeeded]; } completion:nil]; } @end
ViewController.m(保持原样即可)
#import "ViewController.h" #import "MyCustomView.h" @interface ViewController (){ MyCustomView *myView; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; myView = [MyCustomView new]; myView.translatesAutoresizingMaskIntoConstraints = NO; [self.view addSubview:myView]; [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[myView]|" options:0 metrics:nil views:@{@"myView" : myView}]]; [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[myView(600)]" options:0 metrics:nil views:@{@"myView" : myView}]]; } @end
额外说明
如果以后你需要修改的是约束的constant值(而不是multiplier),可以直接修改现有约束的属性,不用替换约束,比如:
self.myLblCenterXConstraint.constant = 50; [self layoutIfNeeded];
但multiplier是只读属性,所以必须通过失效旧约束+创建新约束的方式来修改。
内容的提问来源于stack exchange,提问作者Mohammad Ashraf Ali
相关产品推荐
相关产品推荐

