gpt4 book ai didi

ios - 如何灵活地将 UIbuttons 定位在表格中?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:37:09 26 4
gpt4 key购买 nike

我需要在垂直列表中像这样灵活地定位 UIButton:

A
B
C
D

问题是它需要灵活,就像缺少 B 时它应该看起来像:

A
C
D

因此不应有空格,但 UIButton 应向上移动

现在我像这样以编程方式定位 UIButton,但这只有在我关闭 Autolayout 时才能在 iOS7 中实现

但这反过来又需要定位所有其他元素 - 特别是在屏幕底部 - 也是以编程方式,我不想这样做。

如何以这种方式定位 UIButton,而不必以编程方式在屏幕上以编程方式定位所有其他元素。

Android 中有 tablelayoutlinearLayout 可以自动处理!

编辑:

这很痛苦,但这就是它的工作原理:

  1. position all your btns nicely left aligned, under each other so you can see them nicely in IB. at this stage the vertical spacing does not matter since we delete all these constranints created by IB anyway and programmatically add the vertical spacing we want later
  2. make sure now that there are only the constraints you want, and NO constraints created by IB that you do not want.
  3. In particular remove all vertical spacing constraints between the btns you want to programmatically reposition. Add the constraints you DO want one by one with IB by clicking on "Add constraint" in the grey popup. After that do not touch your layout anymore - you can easily mess it up!
  4. Use this code to reposition now A in relation to C for example. don't forget to set B invisible
UIView *superview = self.view;

NSMutableArray *mutableConstraintsArray =[[NSMutableArray alloc]init];
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:A
attribute:NSLayoutAttributeBottom

relatedBy:NSLayoutRelationEqual toItem:C

attribute:NSLayoutAttributeTop multiplier:1.0 constant:-20.0];//C is 20 points under A
[mutableConstraintsArray addObject:constraint];


for (int i = 0; i<mutableConstraintsArray.count; i++) {
[superview addConstraint:mutableConstraintsArray[i]];
}

我在这里使用了mutableConstraintsArray,所以我可以在此时添加多个约束!

很多努力恕我直言!

如果有更简单的方法请指正/改进!

感谢马特为我指明了正确的方向。

最佳答案

Now I programmatically position the UIButtons programmatically like this, but this is only possible in iOS7 if I turn Autolayout off

那不是真的。您还可以以编程方式使用自动布局约束。假设删除的按钮 B 对其上方的按钮 A 具有约束,对其下方的按钮 C 具有约束。当您删除 B 时,在 A 和 C 之间(以编程方式)创建一个约束,并将该约束添加到它们的公共(public)按钮监督。完毕! C 现在移动到位,D 移动到位,其余跟随。

关于ios - 如何灵活地将 UIbuttons 定位在表格中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21354044/

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