ios - UIStackView - 隐藏堆栈 View 时的布局约束问题

标签 ios swift uistackview

我的应用有 2 个屏幕:

  1. TableViewVC(这里没有堆栈 View )

  2. DetailVC(此处为所有嵌套堆栈 View ;请参阅图片链接:Nested StackViews Picture)——请注意,这些堆栈 View 中包含标签和图像。

当您按下表格 View 中的单元格时,它会将信息从 TableViewVC 传递到 DetailVC。问题在于在 DetailVC 中隐藏特定的 UIStackViews。 我只想在 DetailVC 中的各种 View 中的 2 个堆栈 View 在 View 加载后立即隐藏。所以我在 DetailVC 中编写了这段代码来完成这个:

override func viewDidLoad() {
    super.viewDidLoad()

    self.nameLabel.text = "John"

    self.summaryStackView.hidden = true
    self.combinedStackView.hidden = true
}

一切看起来都不错,但 Xcode 仅在运行时 给出许多警告。当应用程序未运行时,Storyboard 中没有警告。请查看错误图片链接:Picture of Errors

基本上是很多 UISV 隐藏、UISV 间距、UISV Canvas 连接错误。如果我在 viewDidAppear 中隐藏相同的堆栈 View ,这些错误就会消失,但随后应该隐藏的内容一闪而过,然后又隐藏了。用户短暂地看到 View ,然后隐藏起来,这是不好的。

很抱歉不能真正发布图片而不是链接,仍然不能这样做。

关于如何解决这个问题有什么建议吗?这是我真正想发布到应用商店的应用程序 - 这是我的第一个应用程序,所以任何帮助都会很棒!

编辑/更新 1:

我发现了一个小的变通办法,我将这段代码放在了名为 DetailVC 的第二个屏幕中:

// Function I use to delay hiding of views
func delay(delay: Double, closure: ()->()) {
    dispatch_after(
        dispatch_time(
            DISPATCH_TIME_NOW,
            Int64(delay * Double(NSEC_PER_SEC))
        ),
        dispatch_get_main_queue(), closure)
}

// Hide the 2 stack views after 0.0001 seconds of screen loading
override func awakeFromNib() {
    delay(0.001) { () -> () in
        self.summaryStackView.hidden = true
        self.combinedStackView.hidden = true
    }
}

// Update view screen elements after 0.1 seconds in viewWillAppear
override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    delay(0.1) { () -> () in
        self.nameLabel.text = "John"
    }
}

这从 Xcode 中完全消除了关于布局约束的警告。

它仍然不完美,因为有时我会瞥见本应隐藏的 View ——它们在屏幕上快速闪烁然后消失。不过,这种情况发生得如此之快。

关于为什么这会消除警告的任何建议?另外,关于如何改进它以使其完美运行的任何建议???谢谢!

最佳答案

我遇到了同样的问题,我通过将最初隐藏 View 的高度限制设置为 999 的优先级来修复它。

enter image description here

问题是您的 stackview 在隐藏 View 上应用了 0 的高度约束,这与您的其他高度约束冲突。这是错误信息:

Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x7fa3a5004310 V:[App.DummyView:0x7fa3a5003fd0(40)]>",
    "<NSLayoutConstraint:0x7fa3a3e44190 'UISV-hiding' V:[App.DummyView:0x7fa3a5003fd0(0)]>"
)

降低高度限制的优先级可以解决这个问题。

关于ios - UIStackView - 隐藏堆栈 View 时的布局约束问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33642888/

相关文章:

ios - Swift 中的 Double to String 错误

iOS :Can I use custom URL scheme to communicate a flag between three applications should be always in background

ios - 如何设置 Firebase 规则/[FirebaseDatabase] 使用未指定的索引

ios - 在 Xcode 中添加自定义云容器 ID 的位置(不使用开发者帐户)

SwiftUI:如何让 TextField 成为第一响应者?

swift - 通过 UIStackView 将自定义大小的 UILabels 排列在同等大小的 UIImages 下

ios - 在具有后台功能的 iOS 应用程序中使用重新打开的标准文件描述符?

ios - 如何将NSArray保存到xml

ios - UIStackView 是否可以滚动?

ios - UIStackView 没有正确安排 subview