繁体   English   中英

更改约束常量的正确方法是什么

[英]What is the correct way to change constraint constants

我必须更改约束,但是通过更改约束常量,我会得到重复-Xcode发出警告。

我可以看到一个约束是旧约束,另一个是新约束,它们的内存地址不同。

创建和设置视图以处理约束更改的正确方法是什么?

在我的示例中,我有一个mainView添加到self.view中,self.view是扩展UIViewController时提供的视图。 提供了“ self.view”,没有设置约束。

Main MainView是我的容器,每当键盘出现时,我都希望将其高度调整到键盘上方。

viewDidLoad:

- (void)viewDidLoad {

   [super viewDidLoad];

   _mainView = [UIView new];
   _mainView.translatesAutoresizingMaskIntoConstraints = NO;
   [self.view addSubview:_mainView];
}

viewWillLayoutSubviews:

-(void)viewWillLayoutSubviews {

 [super viewWillLayoutSubviews];

 //moved to viewDidLoad
 //self.mainView.translatesAutoresizingMaskIntoConstraints = NO;

//top
[[self view] addConstraint:[NSLayoutConstraint constraintWithItem:_mainView
                                                        attribute:NSLayoutAttributeTop
                                                        relatedBy:NSLayoutRelationEqual
                                                           toItem:self.view
                                                        attribute:NSLayoutAttributeTop
                                                       multiplier:1.0
                                                         constant:0]];

//bottom
_mainBottomContraint = [NSLayoutConstraint constraintWithItem:_mainView
                                                    attribute:NSLayoutAttributeBottom
                                                    relatedBy:NSLayoutRelationEqual
                                                       toItem:self.view
                                                    attribute:NSLayoutAttributeBottom
                                                   multiplier:1.0
                                                     constant:_keyboardOffset];

[[self view] addConstraint:_mainBottomContraint];

//left
[[self view] addConstraint:[NSLayoutConstraint constraintWithItem:_mainView
                                                        attribute:NSLayoutAttributeLeft
                                                        relatedBy:NSLayoutRelationEqual
                                                           toItem:self.view
                                                        attribute:NSLayoutAttributeLeft
                                                       multiplier:1.0
                                                         constant:0]];

//right
[[self view] addConstraint:[NSLayoutConstraint constraintWithItem:_mainView
                                                        attribute:NSLayoutAttributeRight
                                                        relatedBy:NSLayoutRelationEqual
                                                           toItem:self.view
                                                        attribute:NSLayoutAttributeRight
                                                       multiplier:1.0
                                                         constant:0]];

--

每当我更改约束常量时,都会得到重复的约束,viewWillLayoutSubviews会再次被调用并重新创建约束。 如何在不重新创建的情况下更改约束? 或在这里使用的正确模式是什么。

keyboardWillShow-更改约束(setConstant)

- (void)keyboardWillShow:(NSNotification*)aNotification {

    if (![_chatTextField isFirstResponder]) {
        return;
    }

    CGSize tabBarSize = [[[self tabBarController] tabBar] bounds].size;    
    NSDictionary* info = [aNotification userInfo];

    NSTimeInterval duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    UIViewAnimationCurve curve = [[info objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];

    CGSize kbSize = [info[UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    CGRect bkgndRect = _mainView.frame;
    bkgndRect.size.height -= kbSize.height-tabBarSize.height;

    //animate with keyboard
    _keyboardOffset = -kbSize.height-tabBarSize.height;
    [_mainBottomContraint setConstant:_keyboardOffset];
    [self.view setNeedsLayout];

    [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionBeginFromCurrentState | curve     animations:^{
        [_mainView layoutIfNeeded];

    } completion:nil];
}

日志

内存地址0x174286450的约束1

约束2位于内存地址0x174288660

(
"<NSLayoutConstraint:0x174286450 UIView:0x14fd5e5b0.bottom == UIView:0x14fe5edd0.bottom - 271   (active)>",
"<NSLayoutConstraint:0x174288660 UIView:0x14fd5e5b0.bottom == UIView:0x14fe5edd0.bottom   (active)>"
)

在我看来,您要添加_mainBottomContraint两次。首先,创建一个_mainBottomContraint并保留它,当键盘更改时,_mainBottomContraint.containts会更改,并且如果self.view.subviews中的视图更改了框架,系统将发布一个通知,然后视图控制器将接收该通知,并调用viewWillLayoutSubviews,就像视图将调用layoutSubviews。因此,您创建了一个_mainBottomContraint,因此,现在有两个bottomContraint并收到警告。 您只需添加一次约束,就可以在viewDidLoad函数中添加约束,这样会很好。

呼叫

subview.translatesAutoresizingMaskIntoConstraints = NO;

在将子视图添加到另一个视图之前。 否则,会自动创建一些约束,导致出现警告。

- (void)viewDidLoad { 
[super viewDidLoad]; _mainView = [UIView new]; 
self.mainView.translatesAutoresizingMaskIntoConstraints = NO; 

[self.view addSubview:_mainView]; 
}

然后保存您创建的约束的引用(就像您一样),并在需要时更改它们。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM