为什么有限制?

时间:2015-05-16 18:12:17

标签: autolayout visual-format-language

我正在尝试学习视觉格式语言,我试图找出一系列突破性的自动布局约束。

    /**
 * @Name: setUpContainerViews
 * @Description: This is will set up the different 
 * @Parameters: None
 * @Returns: void
 * @Throws: No Exception
 */

- (void)setUpContainerViews {
    tallContainer = [[UIView alloc] init];
    self.topContainerView = [[UIView alloc] init];
    self.bottomContainerView = [[UIView alloc] init];
    self.leftContainerView = [[UIView alloc] init];
    self.rightContainerView = [[UIView alloc] init];

    // We need to add the subviews here because auto layout needs them to be a part of the view.
    [self.view addSubview: tallContainer];
    [self.view addSubview: self.rightContainerView];

    [tallContainer addSubview: self.topContainerView];
    [tallContainer addSubview: self.leftContainerView];
    [tallContainer addSubview: self.bottomContainerView];

    [self.topContainerView setBackgroundColor: [UIColor blueColor]];
    [self.rightContainerView setBackgroundColor: [UIColor orangeColor]];
    [self.leftContainerView setBackgroundColor: [UIColor greenColor]];
    [self.bottomContainerView setBackgroundColor: [UIColor lightGrayColor]];
}

/**
 * @Name: setUpLayoutConstraints
 * @Description: This will set up the layout constraints
 * @Parameters: none
 * @Returns: void 
 * @Throws: No Exception
 */

- (void)setUpLayoutConstraints {

    // Check if all three properties exist
    NSMutableArray *constraintArray = [NSMutableArray array];

    NSDictionary *views = @{
                                @"tallContainer":tallContainer,
                                @"topContainer":self.topContainerView,
                                @"leftContainer":self.leftContainerView,
                                @"rightContainer":self.rightContainerView,
                                @"bottomContainer":self.bottomContainerView
                           };

    // This is for the self.view
    [constraintArray addObjectsFromArray: [NSLayoutConstraint constraintsWithVisualFormat: @"H:|-(0)-[tallContainer]-(0)-[rightContainer(50)]-(0)-|" options:0 metrics:nil views: views]];

    [constraintArray addObjectsFromArray: [NSLayoutConstraint constraintsWithVisualFormat: @"V:|-(0)-[tallContainer]-(0)-|" options:0 metrics:nil views: views]];
    [constraintArray addObjectsFromArray: [NSLayoutConstraint constraintsWithVisualFormat: @"V:|-(0)-[rightContainer]-(0)-|" options:0 metrics:nil views: views]];

    // This is for the tall container
    [constraintArray addObjectsFromArray: [NSLayoutConstraint constraintsWithVisualFormat: @"V:|-(0)-[topContainer(75)]-(0)-[leftContainer]-(0)-[bottomContainer(75)]-(0)-|" options:0 metrics:nil views: views]];

    [constraintArray addObjectsFromArray: [NSLayoutConstraint constraintsWithVisualFormat: @"H:|-(0)-[topContainer]-(0)-|" options:0 metrics:nil views: views]];
    [constraintArray addObjectsFromArray: [NSLayoutConstraint constraintsWithVisualFormat: @"H:|-(0)-[leftContainer]-(0)-|" options:0 metrics:nil views: views]];
    [constraintArray addObjectsFromArray: [NSLayoutConstraint constraintsWithVisualFormat: @"H:|-(0)-[bottomContainer]-(0)-|" options:0 metrics:nil views: views]];

    // This will add the constraints
    [self.view addConstraints: [constraintArray copy]];
}

这些是首先需要更多的破坏限制......

       2015-05-16 11:08:34.222 HighSchoolUMS[4776:1420732] Unable to simultaneously satisfy constraints.
        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) 
    (
        "<NSLayoutConstraint:0x7fe898e11760 V:|-(0)-[UIView:0x7fe898e0f210]   (Names: '|':UIView:0x7fe898e0f100 )>",
        "<NSLayoutConstraint:0x7fe898e117e0 V:[UIView:0x7fe898e0f210(75)]>",
        "<NSAutoresizingMaskLayoutConstraint:0x7fe898d2bb80 h=--& v=--& UIView:0x7fe898e0f210.midY ==>"
    )

    Will attempt to recover by breaking constraint 
    <NSLayoutConstraint:0x7fe898e11760 V:|-(0)-[UIView:0x7fe898e0f210]   (Names: '|':UIView:0x7fe898e0f100 )>

    Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
    The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

我不知道为什么他们打破这似乎是一件简单的事我在这里错过了什么? (显然我是)这个应用程序在横向方面是有帮助的。

0 个答案:

没有答案