在子视图中设置iOS约束

时间:2014-12-29 01:43:49

标签: ios objective-c iphone uiview constraints

我对约束感到陌生,所以我有点失去正确的设置。我尝试做的是使用约束设置主视图,并在该视图内部添加一个视图控制器,然后我可以使用该视图控制器的视图属性来约束该视图控制器内的对象。我已经设置了一个简单的程序来演示我想要做的事情。

viewcontroller.m

#import "ViewController.h"
#import "SearchBarViewController.h"

@interface ViewController (){
    UIView *mainView;
    UIView *topView;
    NSDictionary *viewsDictionary;
    NSDictionary *metrics;
    SearchBarViewController *sbvcSearchBarViewController;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //mainView---------------------------------------------------------------
    mainView = [[UIView alloc] init];
    [mainView setBackgroundColor:[UIColor grayColor]];
    mainView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:mainView];
    //-------------------------------
    viewsDictionary = @{@"mainView": mainView};
    metrics = @{@"vSpacing":@20, @"hSpacing":@5};

    //topView----------------------------------------------------------------
    topView = [[UIView alloc] init];
    [topView setBackgroundColor:[UIColor darkGrayColor]];
    topView.translatesAutoresizingMaskIntoConstraints = NO;
    [mainView addSubview:topView];

    [self createMainConstraints];

    [self createSearchBar];
}

-(void)createMainConstraints{
    //Define the view Position and automatically the Size (for the mainView)
    NSArray *constraint_POS_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-vSpacing-[mainView]-vSpacing-|"
                                                                    options:0
                                                                    metrics:metrics
                                                                      views:viewsDictionary];

    NSArray *constraint_POS_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-hSpacing-[mainView]-hSpacing-|"
                                                                    options:0
                                                                    metrics:metrics
                                                                         views:viewsDictionary];
    [self.view addConstraints:constraint_POS_V];
    [self.view addConstraints:constraint_POS_H];

    //TopBarContainer Constraints ------------------------------------------------------
    [self.view addConstraint:[NSLayoutConstraint
                          constraintWithItem:topView
                          attribute:NSLayoutAttributeWidth
                          relatedBy:NSLayoutRelationEqual
                          toItem:mainView
                          attribute:NSLayoutAttributeWidth
                          multiplier:1
                          constant:0.0]];

    [self.view addConstraint:[NSLayoutConstraint
                          constraintWithItem:topView
                          attribute:NSLayoutAttributeHeight
                          relatedBy:NSLayoutRelationEqual
                          toItem:mainView
                          attribute:NSLayoutAttributeHeight
                          multiplier:.20
                          constant:0.0]];
    //xLoc / yLoc
    [self.view addConstraint:[NSLayoutConstraint
                          constraintWithItem:topView
                          attribute:NSLayoutAttributeCenterX
                          relatedBy:NSLayoutRelationEqual
                          toItem:mainView
                          attribute:NSLayoutAttributeCenterX
                          multiplier:1.0
                          constant:0.0]];

    [self.view addConstraint:[NSLayoutConstraint
                          constraintWithItem:topView
                          attribute:NSLayoutAttributeTop
                          relatedBy:NSLayoutRelationEqual
                          toItem:mainView
                          attribute:NSLayoutAttributeTop
                          multiplier:1.0
                          constant:0.0]];
}

-(void)createSearchBar{
    //SearchBarViewController-----------------------------------------------------------
    sbvcSearchBarViewController = [[SearchBarViewController alloc] init];
    sbvcSearchBarViewController.view.translatesAutoresizingMaskIntoConstraints = NO;
    [topView addSubview:sbvcSearchBarViewController.view];

    //sbvcSearchBarViewController Constraints --------------------------------------
    [self.view addConstraint:[NSLayoutConstraint
                          constraintWithItem:sbvcSearchBarViewController.view
                          attribute:NSLayoutAttributeWidth
                          relatedBy:NSLayoutRelationEqual
                          toItem:topView
                          attribute:NSLayoutAttributeWidth
                          multiplier:1
                          constant:0.0]];

    [self.view addConstraint:[NSLayoutConstraint
                          constraintWithItem:sbvcSearchBarViewController.view
                          attribute:NSLayoutAttributeHeight
                          relatedBy:NSLayoutRelationEqual
                          toItem:topView
                          attribute:NSLayoutAttributeHeight
                          multiplier:1
                          constant:0.0]];
    //xLoc / yLoc
    [self.view addConstraint:[NSLayoutConstraint
                          constraintWithItem:sbvcSearchBarViewController.view
                          attribute:NSLayoutAttributeCenterX
                          relatedBy:NSLayoutRelationEqual
                          toItem:topView
                          attribute:NSLayoutAttributeCenterX
                          multiplier:1.0
                          constant:0.0]];

    [self.view addConstraint:[NSLayoutConstraint
                          constraintWithItem:sbvcSearchBarViewController.view
                          attribute:NSLayoutAttributeCenterY
                          relatedBy:NSLayoutRelationEqual
                          toItem:topView
                          attribute:NSLayoutAttributeCenterY
                          multiplier:1.0
                          constant:0.0]];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

SearchBarViewController.m

#import "SearchBarViewController.h"

@interface SearchBarViewController (){
    UIView *searchBarSubView;
}

@end

@implementation SearchBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor purpleColor]];
    //
    searchBarSubView = [[UIView alloc] init];
    [searchBarSubView setBackgroundColor:[UIColor redColor]];
    [self.view addSubview:searchBarSubView];
    [self addConstraints];
}

- (void)addConstraints {
    [self.view addConstraint:[NSLayoutConstraint
                          constraintWithItem:searchBarSubView
                          attribute:NSLayoutAttributeWidth
                          relatedBy:NSLayoutRelationEqual
                          toItem:self.view
                          attribute:NSLayoutAttributeWidth
                          multiplier:1
                          constant:0.0]];

    [self.view addConstraint:[NSLayoutConstraint
                          constraintWithItem:searchBarSubView
                          attribute:NSLayoutAttributeHeight
                          relatedBy:NSLayoutRelationEqual
                          toItem:self.view
                          attribute:NSLayoutAttributeHeight
                          multiplier:1
                          constant:0.0]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end

SearchBarViewController.m的视图被限制在viewcontroller中,这可以正常工作。但是当我尝试使用SearchBarViewController.m中的约束视图属性来约束searchBarSubView时,我得到了这个讨厌的编译错误:

"无法同时满足约束条件。     可能至少以下列表中的一个约束是您不想要的约束。试试这个:(1)看看每个约束,并试着找出你不期望的东西; (2)找到添加了不需要的约束或约束的代码并修复它。 (注意:如果您正在查看您不了解的NSAutoresizingMaskLayoutConstraints,请参阅UIView属性的文档translatesAutoresizingMaskIntoConstraints) (     "&#34 ;,     "&#34 ;,     "&#34 ;,     "&#34 ;,     "&#34 ;,     "&#34 ;,     "" )

将尝试通过违反约束来恢复  "

在过去的几天里,我已经多次看到同样的错误处理约束,我做到了这一点,但现在我感到茫然。请帮忙!

谢谢!

0 个答案:

没有答案