gpt4 book ai didi

ios - UIButton 文本仅在触摸时出现

转载 作者:可可西里 更新时间:2023-11-01 01:05:52 26 4
gpt4 key购买 nike

我设置了一个 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com