ios - 单击或点击时 View Controller 中的元素消失

标签 ios swift uiviewcontroller viewcontroller

我正在使用 Swift 2 在 XCode 上开发应用程序,但我的 UI 遇到了错误。我在感兴趣的 View Controller 上放置了 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 重新检查所有约束并根据类引用重新配置 View :

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(),那么问题可能会消失。

关于ios - 单击或点击时 View Controller 中的元素消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36345483/

相关文章:

ios - 具有通用解码类型的网络调用

iphone - iPhone/iPad UIWebView 是否需要激活 View 才能呈现?

ios - 快速设置图像中心饼图

swift - 使用 Swift 将用户输入从 TableViewController 复制到 ViewController

iOS - 一个 SegmentedControl - 多个 ViewControllers

javascript - 使用 HTML5/javascript 校正音频音量输出分贝,关闭预处理

iphone - 在 ios 中使用 UITableViewStylePlain 避免标题在 UITableView 中滚动?

ios - 如何清除 NSMutableAttributedString 中的属性?

ios - UITableView 在更改约束后会影响单元格的高度,最终会破坏约束

具有多个 View / subview 的iPhone应用程序: memory is not being deallocated