问题描述
我在主故事板上的设置很简单:
I have a fairly simple set up in my main storyboard:
- 包括三个视图的堆栈视图
- 第一个视图具有固定的高度,并包含一个段控制器
- 另外两个视图没有限制,其想法是一次只激活一个视图,从而填补了可用空间
我有用于处理更改后的视图活动视图的代码,如下所示:
I have code that will deal with the changing view active views as follows:
import Foundation
import UIKit
class ViewController : UIViewController {
@IBOutlet weak var stackView: UIStackView!
@IBOutlet weak var segmentController: UISegmentedControl!
@IBAction func SegmentClicked(_ sender: AnyObject) {
updateView(segment: sender.titleForSegment(at: sender.selectedSegmentIndex)!)
}
override func viewDidLoad() {
updateView(segment: "First")
}
func updateView(segment: String) {
UIView.animate(withDuration: 1) {
if(segment == "First") {
self.stackView.arrangedSubviews[1].isHidden = false
self.stackView.arrangedSubviews[2].isHidden = true
} else {
self.stackView.arrangedSubviews[1].isHidden = true
self.stackView.arrangedSubviews[2].isHidden = false
}
print("Updating views")
print("View 1 is \(self.stackView.arrangedSubviews[1].isHidden ? "hidden" : "visible")")
print("View 2 is \(self.stackView.arrangedSubviews[2].isHidden ? "hidden" : "visible")")
}
}
}
你可以看到,当选择所谓的第一"的标签,索引1的子视图应该显示,虽然2被隐藏,并选择别的时候,索引2的子视图应该显示,虽然1被隐藏
As you can see, when the tab called 'First' is selected, the subview at index 1 should show, whilst 2 is hidden, and when anything else is selected, the subview at index 2 should show, whilst 1 is hidden.
如果我慢慢地改变视图,但是乍一看,这似乎起初作用,但是如果我快一点,单击几次索引后索引1的视图似乎将永久隐藏,从而导致索引0的视图覆盖了整个视图屏幕.我在下面放了一个动画来显示问题和分镜脚本的屏幕截图.输出显示出问题发生时,单击第一段时两个视图都保持隐藏.
This appears to work at first, if I go slowly changing views, but if I go a bit quicker, the view at index 1 seems to remain permanently hidden after a few clicks, resulting in the view at index 0 covering the whole screen. I've placed an animation showing the issue and a screenshot of the storyboard below. The output shows that when the problem happens, both views remain hidden when clicking on the first segment.
有人可以告诉我为什么会这样吗?这是一个错误,还是我没有做应做的事情?
Can anybody tell me why this is happening? Is this a bug, or am I not doing something I should be?
非常感谢!
更新:通过依次进入第一">第二">第三">第二">第一"段,我似乎能够可靠地重现该问题.
Update: I seem to be able to reliably reproduce the issue by going to the First > Second > Third > Second > First segments in that order.
推荐答案
最后,在尝试了所有建议之后,我仍然无法弄清楚为什么会这样,所以我联系了问我的苹果公司.提交错误报告.但是,我确实找到了解决方法,首先取消隐藏了这两种视图,从而解决了我的问题:
In the end, after trying all the suggestions here I still couldn't work out why it was behaving like this so I got in touch with Apple who asked me to file a bug report. I did however find a work around, by unhiding both views first, which solved my problem:
func updateView(segment: String) {
UIView.animate(withDuration: 1) {
self.stackView.arrangedSubviews[1].isHidden = false
self.stackView.arrangedSubviews[2].isHidden = false
if(segment == "First") {
self.stackView.arrangedSubviews[2].isHidden = true
} else {
self.stackView.arrangedSubviews[1].isHidden = true
}
}
}
这篇关于Swift:从stackView中消失视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!