Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在iOS中使用自动布局更改uiview的框架?_Ios_Objective C_Uiview_Constraints - Fatal编程技术网

如何在iOS中使用自动布局更改uiview的框架?

如何在iOS中使用自动布局更改uiview的框架?,ios,objective-c,uiview,constraints,Ios,Objective C,Uiview,Constraints,我在XIB中添加了一个视图,它被设置为自动布局。但当运行时,我想更改该视图的框架。我知道如果使用自动布局,需要在我的代码中设置translatesAutoresizingMaskIntoConstraints=YES。我已经这样做了,我为该视图创建了新的框架,但它在控制台中显示了警告消息: Probably at least one of the constraints in the following list is one you don't want. Try this: (1)

我在XIB中添加了一个视图,它被设置为自动布局。但当运行时,我想更改该视图的框架。我知道如果使用自动布局,需要在我的代码中设置
translatesAutoresizingMaskIntoConstraints=YES
。我已经这样做了,我为该视图创建了新的框架,但它在控制台中显示了警告消息:

    Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSIBPrototypingLayoutConstraint:0x16e4b750 'IB auto generated at build time for view with fixed frame' V:|-(0)-[UIView:0x16ed3c30]   (Names: '|':UIView:0x16ee06b0 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x16e19e30 h=-&- v=-&- UIView:0x16ed3c30.midY == UIView:0x16ee06b0.midY + 44>",
    "<NSAutoresizingMaskLayoutConstraint:0x16e19e60 h=-&- v=-&- UIView:0x16ed3c30.height == UIView:0x16ee06b0.height>"
)

Will attempt to recover by breaking constraint 
<NSIBPrototypingLayoutConstraint:0x16e4b750 'IB auto generated at build time for view with fixed frame' V:|-(0)-[UIView:0x16ed3c30]   (Names: '|':UIView:0x16ee06b0 )>

Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

我如何修复这个bug?请给我一些建议。非常感谢。

如果我理解正确,我认为您需要在类中使用存储的NSLayoutConstraint变量,然后将约束的常量值更新为0,以使UIView滑到顶部

此外,使用自动布局时,通常会设置:

myView.translateAutoResizingMaskIntoConstraint = NO;
那是否定的,而不是肯定的

例如:

@interface MyViewController()
{
    NSLayoutConstraint *myViewTopConstraint;
}

@end

@implementation MyViewController

-(void)setupMyConstraintsMethod
{
    ...

    // ---------------------------------------------------------------
    // This constraint here says "myView" top edge should be pinned
    // to the top edge of the view controller's view but with an offset
    // of 50 pixels. 
    // ---------------------------------------------------------------
    NSLayoutConstraint *myViewTopConstraint = [NSLayoutConstraint constraintWithItem:self.myView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:50.0];

    [self.view addConstraint:myViewTopConstraint];
}

-(void)buttonPressed:(id)sender
{
    // ------------------------------------------------------------
    // now you want myView's top edge to be at the top of your
    // view controller's view, so you set the constant to 0
    // ------------------------------------------------------------
    myViewTopConstraint.constant = 0;

    // animates the sliding up of "myView"
    [UIView animateWithDuration:0.5 animations:^{
        [self.view layoutIfNeeded];
    }];
}

@end

谢谢,但这不是工作。我尝试了您的代码,但仍然显示该警告。您是否有冲突的额外XIB文件约束?
@interface MyViewController()
{
    NSLayoutConstraint *myViewTopConstraint;
}

@end

@implementation MyViewController

-(void)setupMyConstraintsMethod
{
    ...

    // ---------------------------------------------------------------
    // This constraint here says "myView" top edge should be pinned
    // to the top edge of the view controller's view but with an offset
    // of 50 pixels. 
    // ---------------------------------------------------------------
    NSLayoutConstraint *myViewTopConstraint = [NSLayoutConstraint constraintWithItem:self.myView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:50.0];

    [self.view addConstraint:myViewTopConstraint];
}

-(void)buttonPressed:(id)sender
{
    // ------------------------------------------------------------
    // now you want myView's top edge to be at the top of your
    // view controller's view, so you set the constant to 0
    // ------------------------------------------------------------
    myViewTopConstraint.constant = 0;

    // animates the sliding up of "myView"
    [UIView animateWithDuration:0.5 animations:^{
        [self.view layoutIfNeeded];
    }];
}

@end