ios - UIView 没有隐藏,但它里面的按钮是?

标签 ios swift uiview uibutton hidden

我有一个 UIButton,它的文本设置为 "Play":

@IBOutlet weak var boatTypeGamePlayButton: UIButton!

当这个按钮被点击并且它的文本被设置为"play"时,我想执行一些功能:

@IBAction func boatTypeMatchingGameButtonTapped(sender: AnyObject) {
    if boatTypeGamePlayButton.titleLabel?.text == "Play" {
        boatTypeMatchingGameLabel.hidden = true
        boatTypeGameLevelContainer.hidden = false
        boatTypeMatchingGameTitle.text = "Select Level"
        boatTypeGamePlayButton.setTitle("Cancel", forState: UIControlState.Normal)
    }

boatTypeGameLevelContainer 是一个 UIView,里面有四个按钮。

问题是 boatTypeGameLevelContainer 确实变得可见,它应该是可见的,但其中的按钮却不可见。我尝试为按钮制作 IBOutlet 并将它们设置为在此函数中可见,但它没有改变任何东西。

我设置的所有其他方法都有效...

我还有一个 else 语句(意思是 boatTypeGamePlayButton 的文本设置为 "Cancel"):

else {
    boatTypeMatchingGameLabel.hidden = false
    boatTypeGameLevelContainer.hidden = true
    boatTypeMatchingGameTitle.text = "Boat Type Matching Game"
    boatTypeGamePlayButton.titleLabel?.text = "Play"
    boatTypeGamePlayButton.setTitle("Play", forState: UIControlState.Normal)
}

如何确保按钮出现?感谢您的支持。

更新:我正在使用 Storyboard添加按钮,并且我没有在代码中更改它们的任何属性。

最佳答案

您在两个语句中添加了相同的标题,这导致了混淆。如果两个实例的文本都是 Play,则会产生一些问题,并且会取消。我建议将条款分开。

@IBAction func boatTypeMatchingGameButtonTapped(sender: AnyObject) {
if boatTypeGamePlayButton.titleLabel?.text == "Play" {
//some code here
}
else if boatTypeGamePlayButton.titleLabel?.text == "Cancel"{
//do your other code here
} else {
//do something if none of those statements are true
}

编辑

如果您声明 View 正在填充并且您的背景显示您在 Storyboard中设置的颜色,则可能是由于缺少自动布局约束。通常,当您为特定大小创建 Storyboard或 xib 并在不同大小上运行时,您指定没有为 View 及其组件设置的约束,它们不会像您看到的那样编译和运行在 Storyboard中。

您应该查看 Auto Layout Guide苹果已经提供。它内容广泛,是您 future 项目的重要资源。如果你学习得当,自动布局是非常直观的。祝你好运

关于ios - UIView 没有隐藏,但它里面的按钮是?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27709122/

相关文章:

iphone - 如何识别一个可靠的 "hw.machine"标识符?

ios - 在 iOS 上,当某些维度信息不可用时,我们如何在 viewDidLoad 中在屏幕上绘制内容?

swift - 在 `kind` 和 "Show": why does the change not take effect when running the simulator? 之间更改 Storyboard segue "ShowDetail"

ios - 旋转 MDRotatingPieChart 更改 x/y 位置而不是角度

iOS:这个初始 View 从哪里来以及如何删除它?

ios - 如何检测 View 中的某些部分

iphone - 在 iOS 上,有没有办法只搜索带有特定标签的 subview ?

ios - 使用属性进行类初始化

ios - 如何制作不规则形状的 UIScrollView?

ios - Swift - 如果检测到 "Swipe to delete",如何隐藏自定义 tableViewCell 类中的按钮?