我设置了一个 UIButton,如下所示:
let scanButton = UIButton()
func setUpScanButton (scanButton: UIButton) -> () {
scanButton.addTarget(self, action: "goToScanner" , forControlEvents: UIControlEvents.TouchUpInside)
scanButton.backgroundColor = UIColor.greenColor()
scanButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
scanButton.setTitle("Scan", forState: UIControlState.Normal)
scanButton.frame = CGRectMake(36, 385, self.view.frame.width - 41, 30)
scanButton.center.x = CGRectGetMidX(self.view.bounds)
scanButton.center.y = CGRectGetMidY(self.view.bounds)
scanButton.layer.cornerRadius = 6
self.view.addSubview(scanButton)
}
setUpScanButton(scanButton)
问题在于,只有触摸按钮后,文本才会显示。我尝试编辑文本和按钮的颜色,但没有成功。
最佳答案
您的按钮很可能是在辅助线程上绘制的。所以它不会在正确的时间绘制。
为了在正确的时间正确绘制,所有 UI 元素都必须在主线程上绘制。
您可以使用以下代码来实现:
dispatch_async(dispatch_get_main_queue(), {
// Insert your code to add the button here
})
在 Swift 3 和 4 中:
DispatchQueue.main.async {
// Insert your code to add the button here
}
关于ios - UIButton 文本仅在触摸时出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30542713/