单击或点击时视图控制器中的元素消失
Elements in View Controller Disappear When Clicking or Tapping Them
我正在 XCode 上使用 Swift 2 开发应用程序,运行 我的 UI 出现错误。我在感兴趣的视图控制器上放置了 UILabel、UITextView 和 UITextField。
在输入 UITextField 之前,一切正常。我插入了文本字段并分配了适当的约束,在模拟应用程序时,单击文本字段将导致所有 UI 元素消失或 alpha 立即变为零(我不确定哪个这两者实际上正在发生)。
我没有通过控制台收到任何反馈,应用程序本身也没有崩溃。
这是我的代码:
//Declare the following UI objects to be manipulated
@IBOutlet weak var labelOneTitle: UILabel!
@IBOutlet weak var textDescription: UITextView!
@IBOutlet weak var fieldInput: UITextField!
override func viewDidLayoutSubviews() {
//Set UI object alphas to zero prior to loading the view
labelOneTitle.alpha = 0
textDescription.alpha = 0
fieldInput.alpha = 0
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewDidAppear(animated: Bool) {
UIView.animateWithDuration(2) { () -> Void in
self.labelOneTitle.alpha = 1
}
UIView.animateWithDuration(3) { () -> Void in
self.textDescription.alpha = 1
}
UIView.animateWithDuration(3) { () -> Void in
self.fieldInput.alpha = 1
}
}
可能是 UIViewController.viewDidLayoutSubviews 根据 class 参考重新检查所有约束并重新配置视图:
When the bounds change for a view controller's view, the view adjusts the positions of its subviews and then the system calls this method. However, this method being called does not indicate that the individual layouts of the view's subviews have been adjusted. Each subview is responsible for adjusting its own layout.
Your view controller can override this method to make changes after
the view lays out its subviews. The default implementation of this
method does nothing.
当您单击 UITextField 时,键盘会出现(可能),从而回忆起将所有 UI 元素的 alpha 设置为 0 的方法。
如果您将 alpha 设置为 0 到 viewDidLoad(),那么问题可能会消失。
我正在 XCode 上使用 Swift 2 开发应用程序,运行 我的 UI 出现错误。我在感兴趣的视图控制器上放置了 UILabel、UITextView 和 UITextField。
在输入 UITextField 之前,一切正常。我插入了文本字段并分配了适当的约束,在模拟应用程序时,单击文本字段将导致所有 UI 元素消失或 alpha 立即变为零(我不确定哪个这两者实际上正在发生)。
我没有通过控制台收到任何反馈,应用程序本身也没有崩溃。
这是我的代码:
//Declare the following UI objects to be manipulated
@IBOutlet weak var labelOneTitle: UILabel!
@IBOutlet weak var textDescription: UITextView!
@IBOutlet weak var fieldInput: UITextField!
override func viewDidLayoutSubviews() {
//Set UI object alphas to zero prior to loading the view
labelOneTitle.alpha = 0
textDescription.alpha = 0
fieldInput.alpha = 0
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewDidAppear(animated: Bool) {
UIView.animateWithDuration(2) { () -> Void in
self.labelOneTitle.alpha = 1
}
UIView.animateWithDuration(3) { () -> Void in
self.textDescription.alpha = 1
}
UIView.animateWithDuration(3) { () -> Void in
self.fieldInput.alpha = 1
}
}
可能是 UIViewController.viewDidLayoutSubviews 根据 class 参考重新检查所有约束并重新配置视图:
When the bounds change for a view controller's view, the view adjusts the positions of its subviews and then the system calls this method. However, this method being called does not indicate that the individual layouts of the view's subviews have been adjusted. Each subview is responsible for adjusting its own layout.
Your view controller can override this method to make changes after the view lays out its subviews. The default implementation of this method does nothing.
当您单击 UITextField 时,键盘会出现(可能),从而回忆起将所有 UI 元素的 alpha 设置为 0 的方法。
如果您将 alpha 设置为 0 到 viewDidLoad(),那么问题可能会消失。