gpt4 book ai didi

uitableview - 自动布局 : How to compress a complex popover

转载 作者:行者123 更新时间:2023-12-03 17:42:47 24 4
gpt4 key购买 nike

如果你喜欢尝试源代码(你是欢迎做),看看我的Bitbucket repository .

我有一个弹出对话框,显示设置列表。这些设置在多个 UITableViews 中列出。 UITableViews 不应是可滚动的,因为整体设置 View 已经是。此外,弹出对话框应根据需要垂直占用尽可能多的屏幕,但应水平压缩。

因此,我构思了以下结构:

UIView => MySettingsViewController
- UIScrollView
- UIView (Content View)
- Container View1
- UITableView (embedded) => MyTableViewController
- Container View2
- UITableView (embedded)

该结构是通过 Interface Builder 组装的,Autolayout 用于调整大小。

我有 ScrollView 、内容 View (我只从一个开始)和容器 View 到它们各自的 super View (或布局指南)。我通过以下方式限制了内容 View 的大小:
contentView.width == (topmost) UIView.width
contentView.height == 200 // removed at build time

此外,我将表 View 的大小设置为其内容大小,否则弹出窗口似乎是空的:

class MyTableViewController: UITableViewController {
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
// this is Cartography syntax - the intention should be clear
layout(view, replace: ConstraintGroup()) { [unowned self] view in
view.width == self.tableView.contentSize.width
view.height == self.tableView.contentSize.height
}
view.setNeedsLayout()
}
}

设置弹出框充满了内容,但它的大小不太合适:

enter image description here

为了解决这个问题,我尝试了以下不起作用的方法:

class MySettingsViewController: UIViewController {
override var preferredContentSize: CGSize {
get {
let compressedSize = view.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize)
// this is always (0, 0) because the subviews are not resized, yet
return compressedSize
}
set {
super.preferredContentSize = newValue
}
}
}

结论:压缩不起作用。

最佳答案

所以我只是自己解决了这个问题,正如您在查看 Bitbucket 存储库时所看到的那样。

布局现在在 MyTableViewController 中都已修复和 MySettingsViewController .前一个现在看起来像这样:

class MyTableViewController: UITableViewController {

var heightConstraint: NSLayoutConstraint?
var tableViewEdgesConstraints: [NSLayoutConstraint]?

override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
if let container = tableView.superview where tableViewEdgesConstraints == nil {
layout(tableView, container, replace: ConstraintGroup()) { [unowned self] tableView, container in
self.tableViewEdgesConstraints = tableView.edges == inset(container.edges, 0)
}
}
}

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()

if let heightConstraint = heightConstraint {
if Int(heightConstraint.constant) != Int(tableView.contentSize.height) {
heightConstraint.constant = self.tableView.contentSize.height
}
} else {
layout(view, replace: ConstraintGroup()) { [unowned self] view in
if (self.tableView.contentSize.height > 0) {
self.heightConstraint = view.height == self.tableView.contentSize.height
}
}
}
}
}

所以基本上,我将表格的高度限制为其内容的高度,并在内容的高度发生变化时更改约束。这是在布置 table 后立即完成的。此外,嵌套表 View 通过其边缘固定到容器 View 的边缘。我认为这是强制性的,因为我无法找到如何在 Interface Builder 中约束不同场景的两个 View 。

MySettingsViewController ScrollView 的大小设置为内容 View 框架的大小(可通过 socket 访问),一旦此大小已知。此外,为了使弹出框压缩,设置 Controller 的 preferredContentSize 会在高度发生变化时相应地进行调整(如果省略该条件,您可能会陷入布局无限循环。此外,我做了 3 件事以使其成为可能环绕在 MySettingsViewController 周围的导航 Controller :
  • 弹出框的宽度设置为固定值(否则有时会扩展到全宽)。
  • presentedViewController 的 preferredContentSize 需要设置相同。
  • 我不得不将 scrollView 的 insets 设置为 0 以避免丑陋的垂直偏移 - 这个解决方案是次优的,因为它有点破坏了 ScrollView 体验。但它有效。

  • 这是代码:

    class MySettingsViewController: UIViewController {

    @IBOutlet weak var contentView: UIView!
    @IBOutlet weak var scrollView: UIScrollView!

    override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    scrollView.contentSize = contentView.frame.size

    if (preferredContentSize.height != scrollView.contentSize.height) {
    let newSize = CGSize(width: 400, height: scrollView.contentSize.height)
    preferredContentSize = newSize
    presentingViewController?.presentedViewController?.preferredContentSize = newSize
    scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0)
    }
    }
    }

    这是结果:

    enter image description here

    关于uitableview - 自动布局 : How to compress a complex popover,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32013571/

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