NSLayoutConstraints并动态设置视图的宽度/高度

时间:2012-10-12 18:25:47

标签: objective-c ios xcode nslayoutconstraint

我有一个关于设置视图大小的问题,我正在应用一些布局约束 如何定义视图的大小而不定义其框架?

我正在尝试创建一个高度固定且在屏幕原点有原点的视图。它填充视图的宽度,以便在设备旋转时视图延伸到屏幕的宽度。这是我到目前为止所得到的......

self.containerView = [[HitTestContainerView alloc] init];
[self.containerView setBackgroundColor:[UIColor redColor]];
[self.view addSubview:self.containerView];

NSDictionary *viewsDictionary = @{@"view":self.containerView};
NSArray *verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-50-[view]-250-|" options:0 metrics:nil views:viewsDictionary];
NSArray *horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[view]|" options:0 metrics:nil views:viewsDictionary];
[self.view addConstraints:verticalConstraints];
[self.view addConstraints:horizontalConstraints];

问题是除非我设置containerView的框架,否则我无法在屏幕上显示任何内容。在不使用Interface Builder的情况下执行此操作的正确方法是什么?

5 个答案:

答案 0 :(得分:48)

我写了一个小工具来帮助你解决这个问题:

http://autolayoutconstraints.com

以下是该工具自动生成的问题的答案

目标C

// align view from the left and right
[self.containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[view]-10-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view)]];

// width constraint
[self.containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[view(==250)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view)]];

// height constraint
[self.containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[view(==50)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view)]];

夫特

// align view from the left and right
self.containerView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-10-[view]-10-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["view": view]));

// width constraint
self.containerView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[view(==250)]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["view": view]));

// height constraint
self.containerView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[view(==50)]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["view": view]));

答案 1 :(得分:45)

要使用布局约束定义视图的高度和宽度,您可以使用布局约束来定义视图的高度和宽度。例如,您已经说过:

NSArray *verticalConstraints = 
[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-50-[view]-250-|" 
    options:0 metrics:nil views:viewsDictionary];

所以你可以改为:

NSArray *verticalConstraints = 
[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(50)-[view(40)]" 
    options:0 metrics:nil views:viewsDictionary];

现在,视图将从其超视图顶部50像素,高40像素。

或者,您可以使用constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:来设置高度限制;当然,toItem将是零,因为与另一个视图没有任何关系。

答案 2 :(得分:7)

晚于永远......

对于Storyboard,IB和Swift:

只需控制 - 将约束像任何其他控件一样拖到viewController

@IBOutlet weak var spaceToTopThatIDecided: NSLayoutConstraint!

然后简单地改变它:

spaceToTopThatIDecided.constant = 44

这就是全部!

答案 3 :(得分:3)

您不必使用addConstraints :()方法

- (void)addConstraint:(NSLayoutConstraint *)constraint NS_AVAILABLE_MAC(10_7);

//不推荐使用此方法,应避免使用此方法。相反,将NSLayoutConstraint的活动属性设置为YES。

- (void)addConstraints:(NSArray *)constraints NS_AVAILABLE_MAC(10_7);   

//不推荐使用此方法,应避免使用此方法。而是使用

+[NSLayoutConstraint activateConstraints:].

//需要使用

+ (void)activateConstraints:(NSArray *)constraints NS_AVAILABLE(10_10, 8_0);

//然后设置为

NSView *view,*containerView;
view =//superview
containerView=//subview
[view addSubview:containerView];

//添加约束

[containerView setTranslatesAutoresizingMaskIntoConstraints:NO];

//关于它从Apple文档中读取

//然后添加像

这样的约束
[NSLayoutConstraint activateConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-50-[view]-50-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(containerView)]];
[NSLayoutConstraint activateConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[view]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(containerView)]];

//就是这样。

答案 4 :(得分:0)

Swift 4: 您可以使用以下2个选项管理宽度和高度,以及前导,尾随和顶部和底部空间

 //1 . left and right spacing
    theContentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-16-[view]-16-|", options:NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["view":nibView]))

 //2. top and bottom spacing
    theContentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-16-[view]-16-|", options:NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["view":nibView]))

OR 更具体一点,并且对W,H,左间距,顶部间距有单独的约束,您可以按照以下方式执行

 //left only spacing
        theContentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-16-[view]", options:NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["view":nibView]))

//top only spacing
        theContentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-16-[view]", options:NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["view":nibView]))

//width
        theContentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:[view(==130)]", options:NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["view":nibView]))

//height
        theContentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[view(==100)]", options:NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["view":nibView]))