gpt4 book ai didi

ios - NSLayoutConstraint 将按钮放置在 View 的中心

转载 作者:行者123 更新时间:2023-11-30 14:10:28 24 4
gpt4 key购买 nike

我正在尝试使用约束,它们看起来很不错:)

我想知道将按钮放置在 View 中心(水平和垂直)的 VisualString 是什么。

我应该使用

NSLayoutContraint(item: attribute: relatedBy: toItem: attribute: multiplier: constant)

最佳答案

Restraint你只需要这样做:

let imageViewHorizontalConstraint = NSLayoutConstraint(
item: imageView,
attribute: .CenterX,
relatedBy: .Equal,
toItem: self,
attribute: .CenterX,
multiplier: 1.0,
constant: 0
)

let imageViewVerticalConstraint = NSLayoutConstraint(
item: imageView,
attribute: .CenterY,
relatedBy: .Equal,
toItem: self,
attribute: .CenterY,
multiplier: 1.0,
constant: 0
)

addConstraints([imageViewHorizontalConstraint, imageViewVerticalConstraint])

没有视觉格式

 // Center horizontally
[self addConstraint:[NSLayoutConstraint constraintWithItem:centerView
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0.0]];

[self addConstraint:[NSLayoutConstraint constraintWithItem:centerView
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0.0]];

视觉格式

// Variables
NSDictionary *views = @{
@"view" : self.view,
@"superview" : self.view.superview,
};
NSDictionary *metrics = @{
@"width" : @(CGRectGetWidth(self.view.frame)),
@"height" : @(CGRectGetHeight(self.view.frame))
};

// Constraints
NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[superview]-(<=1)-[view(width)]"
options:NSLayoutFormatAlignAllCenterY
metrics:metrics
views:views];
[self.view.superview addConstraints:constraints];

constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[superview]-(<=1)-[view(height)]"
options:NSLayoutFormatAlignAllCenterX
metrics:metrics
views:views];
[self.view.superview addConstraints:constraints];

关于ios - NSLayoutConstraint 将按钮放置在 View 的中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31832653/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com