无法设置CALayer的边框颜色 [英] Can't set CALayer's border color

查看:50
本文介绍了无法设置CALayer的边框颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试自定义UIButton的外观,如下所示:

I'm trying to customize the appearance of a UIButton, like this:

@IBOutlet weak var button: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()

    button.layer.cornerRadius = button.frame.size.width / 2
    button.layer.borderColor = UIColor.cyan.cgColor
    button.layer.borderWidth = 1
}

但是,当我运行它时,它仅显示按钮的文字,什么可能导致问题呢?

However when I run it, it displays nothing but the text of the button, what could cause the problem?

推荐答案

问题是您运行此代码的时间过早(在 viewDidLoad 中).您的 cornerRadius 计算取决于 calculateButton.frame ,但目前尚不知道其值.将代码移到 viewDidLayoutSubviews 中,我想您会发现它可以按预期工作.

The problem is that you are running this code too soon (in viewDidLoad). Your cornerRadius calculation depends upon calculateButton.frame, but its value is not known at this time. Move your code into viewDidLayoutSubviews and I think you will find that it works as you expect.

这篇关于无法设置CALayer的边框颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆