如何在Objective C中更新Button Cilck上的UILabel的centerXAnchor

时间:2018-01-20 09:37:21

标签: ios objective-c constraints

我想在按钮上更新UILabel的中心锚点但是它没有更新虽然当我设置ist 1st时在viewDidload它正在工作但是点击不更新它的centerX点。

这是我的示例代码:

MyCustomView.h

 #import <UIKit/UIKit.h>

    @interface MyCustomView : UIView
    @property (nonatomic,strong)UILabel *MyLbl;
    @property (nonatomic,strong)NSLayoutConstraint * MyLblConstraint;
    - (id)initWithFrame:(CGRect)frame;
    @end

MyCustomView.m

    #import "MyCustomView.h"

        @implementation MyCustomView
        @synthesize MyLbl;

        - (id)initWithFrame:(CGRect)frame;
        {
            self = [super initWithFrame:frame];
            if (self) {
        self.translatesAutoresizingMaskIntoConstraints = NO;
        self.backgroundColor = [UIColor whiteColor];
        NSString *LblTxtStr = @"This is My Label Text";

         CGSize LblTxtStrSize = [LblTxtStr sizeWithAttributes: @{NSFontAttributeName:[UIFont fontWithName: @"Helvetica-Bold" size: 18]}];
      CGSize LblFrameSize = CGSizeMake(ceilf(LblTxtStrSize.width), ceilf(LblTxtStrSize.height));  


        self.MyLbl = [UIView new];
        self.MyLbl.translatesAutoresizingMaskIntoConstraints = NO;

        [self addSubview: self.MyLbl];

[self.MyLbl.widthAnchor constraintEqualToConstant: LblFrameSize.width].active = YES;
[self.MyLbl.bottomAnchor constraintEqualToAnchor: self.bottomAnchor].active = YES;
[self.MyLbl.heightAnchor constraintEqualToConstant: self.LblFrameSize.height].active = YES;

self.MyLblConstraint = [NSLayoutConstraint constraintWithItem:self.MyLbl attribute: NSLayoutAttributeCenterX relatedBy: NSLayoutRelationEqual toItem: self attribute: NSLayoutAttributeCenterX multiplier: 0.5 constant: 0];
 self.MyLblConstraint.active = YES;


 UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.translatesAutoresizingMaskIntoConstraints = NO;
    [btn setTitle:@"test 1" forState: UIControlStateNormal];
    [btn addTarget: self action:@selector(MyBtnClick:) forControlEvents: UIControlEventTouchUpInside];
    [self.view addSubview: btn];
    [self.view addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[btn]|" options: 0 metrics: nil views: @{@"btn": btn}]];
    [self.view 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.MyLblConstraint = [self.MyLbl.centerXAnchor constraintEqualToAnchor: self.centerXAnchor];

[UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping: 1 initialSpringVelocity: 1 options:UIViewAnimationOptionCurveEaseOut animations:^{[self layoutIfNeeded];} completion: nil];
}


        @end

现在在My ViewController的viewDidload方法如下:

ViewController.m

#import "ViewController.h"
#import "MyCustomView.h"

@interface INVRShareController (){
 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}]];
}

2 个答案:

答案 0 :(得分:1)

创建约束后,您必须激活它。

self.MyLblConstraint = [self.MyLbl.centerXAnchor constraintEqualToAnchor: self.centerXAnchor].active = true;

*编辑

问题在于您有2个冲突的约束。 解决方案是在viewDidLoad中创建两个约束。然后一次只制作一个约束“活动”。

在viewDidLoad中,

constraint1 = [NSLayoutConstraint constraintWithItem:self.MyLbl attribute: NSLayoutAttributeCenterX relatedBy: NSLayoutRelationEqual toItem: self attribute: NSLayoutAttributeCenterX multiplier: 0.5 constant: 0];
constraint1.active = true
constraint2 = [self.MyLbl.centerXAnchor constraintEqualToAnchor: self.centerXAnchor];

在按钮操作中,

constraint1.active = false
constraint2.active = true

答案 1 :(得分:1)

更改btn点击此

-(void)MyBtnClick:(id)sender {

  [self removeConstraint:self.MyLblConstraint];

  self.MyLblConstraint = [self.MyLbl.centerXAnchor constraintEqualToAnchor:  self.centerXAnchor];

  self.MyLblConstraint.active = YES;

  [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping: 1 initialSpringVelocity: 1 options:UIViewAnimationOptionCurveEaseOut animations:^{[self layoutIfNeeded];} completion: nil];
}