• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-12 18:16:31 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我需要像这样在垂直列表中灵活地定位 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,所以我可以在这个时候添加多个约束!

很多努力恕我直言!

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

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



Best Answer-推荐答案


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) super View 。完毕!现在 C 移动到位,D 移动到位,其余跟随。

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

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap