gpt4 book ai didi

ios - View 和 super View 之间的自动布局标准空间不起作用

转载 作者:行者123 更新时间:2023-11-29 12:25:18 25 4
gpt4 key购买 nike

我正在使用 Auto Layout Visual Format Lanugage使 subview 的框架适合其父 View 的框架,减去标准空间量(大约 8px)(“标准空间”在视觉格式语言中表示为 -)。

这是我的代码:

class ViewController: UIViewController {

var imageView: UIImageView = UIImageView()

override func viewDidLoad() {
super.viewDidLoad()

self.view.backgroundColor = UIColor.redColor()

imageView.backgroundColor = UIColor.greenColor()
imageView.setTranslatesAutoresizingMaskIntoConstraints(false)
self.view.addSubview(imageView)

let viewsDict = ["imageView": imageView]

let imageViewConstraintsH = NSLayoutConstraint.constraintsWithVisualFormat("H:|-[imageView]-|",
options: NSLayoutFormatOptions.allZeros,
metrics: nil,
views: viewsDict)
self.view.addConstraints(imageViewConstraintsH)

let constraintsV = NSLayoutConstraint.constraintsWithVisualFormat("V:|-[imageView]-|",
options: NSLayoutFormatOptions.allZeros,
metrics: nil,
views: viewsDict)
self.view.addConstraints(constraintsV)
}
}

正如您在下面的屏幕截图中所看到的,标准间距是水平的,但不是垂直的:

enter image description here

最佳答案

我无法回答为什么它不起作用(Apple DTS 也不能),但您可以使用 super View 中的内置布局指南来完成同样的事情,如下所示:

- (void)viewDidLoad {
[super viewDidLoad];

id topLayoutGuide = self.topLayoutGuide;
id bottomLayoutGuide = self.bottomLayoutGuide;

UIImageView *imageView = [[UIImageView alloc]init];
[imageView setTranslatesAutoresizingMaskIntoConstraints:false];

NSDictionary *views = NSDictionaryOfVariableBindings(imageView, topLayoutGuide, bottomLayoutGuide);


self.view.backgroundColor = [UIColor grayColor];
imageView.backgroundColor = [UIColor whiteColor];

[self.view addSubview:imageView];

[self.view addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[imageView]-|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(imageView)]
];
[self.view addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat:@"V:[topLayoutGuide]-[imageView]-[bottomLayoutGuide]"
options:0
metrics:nil
views:views]
];

}

感谢 T. Cooper

关于ios - View 和 super View 之间的自动布局标准空间不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29287014/

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