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

ios - 自动布局约束优先级未解决

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

我有一个 View ,它有一个 UILabel、一个 UITableView(tblFilters) 和一个 UIView(btnBaseView)(保留其他三个 UIButtons)。请查看下图:- enter image description here

我需要扩展 tblFilters 高度以展示每个类别的选项,但需要使 btnBaseView 始终在屏幕上可见。所以基本上 tblFilters 高度不应该超过限制。

为了实现这一点,我对 btnBaseView 应用了高度约束,并赋予它必需优先级。同样的方式 tblFilters 有一个高度限制,但有一个 DefaultHigh 优先级。

// Height Constraint of btnBaseView. Height Should always be >=116
  btnSectionHeightConstraint = [NSLayoutConstraint constraintsWithVisualFormat"V:[btnBaseView(>=116)]" options:0 metrics:nil views{@"btnBaseView":btnBaseView}];
  [[btnSectionHeightConstraint firstObject] setPriority:UILayoutPriorityRequired];
  [self addConstraints:btnSectionHeightConstraint];


// TableView Height Constraint. Height value is being changed when user click on "+" button of table section.
  tableHeightConstraints = [NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat"V:[tblFilters(>=%f)]",176.0] options:0 metrics:nil views{@"tblFilters":tblFilters}];
  [[tableHeightConstraints firstObject] setPriority:UILayoutPriorityDefaultLow];
  [self addConstraints:tableHeightConstraints];

但是这个方案似乎不起作用,因为 tableView 覆盖了整个 baseView 并将 btnBaseView 推出了可见区域。

我也尝试将 DefaultLow 的优先级保留为 tblFilters,但没有效果。当我在更改 tblFilters 高度约束后调试代码时,它会在控制台中打印正确的优先级输出,但对 View 没有影响。

有人可以帮我找出约束优先级没有按预期工作的问题,还是我对这个概念有错误的理解。任何帮助表示赞赏。



Best Answer-推荐答案


  1. 设置 tblFilters 高度约束优先级为 UILayoutPriorityDefaultHigh
  2. tblFilters 相关的 btnBaseView 上边距约束设置为 0
  3. 设置 btnBaseViewbottomView 相关的下边距约束 为 >= 0

然后你改变 tblFilters 对应数据的高度约束

tableHeightConstraints.constant = someValue
view.layoutIfNeeded()

enter image description here

关于ios - 自动布局约束优先级未解决,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31426180/

回复

使用道具 举报

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

本版积分规则

关注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