【问题标题】:How to Update centerXAnchor of a UILabel on Button Cilck in Objective C如何在Objective C中单击按钮时更新UILabel的centerXAnchor
【发布时间】:2018-01-20 09:37:21
【问题描述】:

我想在单击按钮时更新 UILabel 的中心锚点,但它没有更新,尽管当我在 viewDidload 中设置 ist 1st Time 时它正在工作,但单击时未更新其 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}]];
}

【问题讨论】:

  • 你在按钮方法中错过了'.isActive = true。

标签: ios objective-c constraints


【解决方案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

【讨论】:

  • 不工作其给出错误:No setter method 'setIsActive:' for assignment to property
  • 当我单击另一个按钮返回到上一个 centerX 点时,它工作但不能正常工作,然后如果我再次单击此按钮,它不会更新其值。
  • 谢谢,@ArunGJ 现在工作正常!!非常感谢。
【解决方案2】:

将 btn click 更改为此

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-24
    • 1970-01-01
    • 2015-12-20
    相关资源
    最近更新 更多