gpt4 book ai didi

iOS 约束 : how to adjust UIButton width and height in a view?

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

我正在尝试制作键盘。我在 View 中有 9 个按钮:宽度和高度 <= 75。在 iPhone 5 上完美运行。但是 iPhone 4 的按钮被拉长了,尺寸仍然是 75。谁能帮帮我?

最佳答案

问题是您告诉 View (按钮)的高度或宽度 >= 75,这意味着您的布局不明确(有人已经提到过这一点)——您可以通过检查 View 的 hasAmbiguousLayout 属性来检查这一点.它可能无法在 iPhone 5 上正常工作,或者碰巧当您运行它时,自动布局已经找到了您正在寻找的解决方案,因此它似乎可以正常工作。运行它足够多次,最终您可能会得到不需要的布局。啊自动布局的乐趣。无论如何,这个问题的一个解决方案是设置一个按钮的高度和宽度,然后告诉所有其他按钮跟随套件。视觉格式语言指南有一个这样的例子,但我会告诉你我的意思。

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[button1(75)[button2(==button1)[button3(==button1)]|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(button1, button2, button3)]];

然后做一些类似的事情来垂直放置它们。当自动布局运行时,它应该为它们找到正确的布局。下一个问题是如何使用它。如果您在 UIView 的子类中完成所有这些工作,然后继续将 View 宽度设置为 400 pt 之类的疯狂宽度,那么自动布局将中断 - 在这种情况下,因为我们固定在左侧和右侧。要解决这个问题,我可能会删除最后一个 |并且不将 button3 的右侧固定到父 View 的右侧。

您的另一个选择是使用长格式指定约束。例如

//set button1 width to 75
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:button1
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1
constant:75]];

//set button2 width == button1
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:button2
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:button1
attribute:NSLayoutAttributeWidth
multiplier:1
constant:0]];

//pin button1 to the left
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:button1
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeft
multiplier:1
constant:0]];

//pin button2 to button1
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:button2
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:button1
attribute:NSLayoutAttributeRight
multiplier:1
constant:0]];

关于iOS 约束 : how to adjust UIButton width and height in a view?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23395386/

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