You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何用变量设置UITextView的layer边框属性时边框不显示?

UITextView边框变量赋值不显示问题

当使用字面量设置UITextView的layer.borderColor和layer.borderWidth属性时,边框正常显示,但使用变量赋值这两个属性时,边框却不显示。

保存属性值的代码及输出

以下代码用于保存UITextView的layer.borderColor和layer.borderWidth值:

override func viewDidLoad() {
    super.viewDidLoad()

    self.textViewBodyBorderColor = self.textViewBody.layer.borderColor

    print("self.textViewBodyBorderColor: \(self.textViewBodyBorderColor)")

    self.textViewBodyBorderWidth = self.textViewBody.layer.borderWidth

    print("self.textViewBodyBorderWidth: \(self.textViewBodyBorderWidth)")

}

调试窗口输出:

self.textViewBodyBorderColor: Optional(UIExtendedGrayColorSpace 0 1)
self.textViewBodyBorderWidth: Optional(1.0)

变量赋值的代码及输出

以下代码在viewWillAppear(_:)中用变量为UITextView对应属性赋值:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    print("self.textViewBodyBorderColor: \(self.textViewBodyBorderColor)\n\tself.textViewBodyBorderWidth: \(self.textViewBodyBorderWidth!)")

    self.textViewBody.layer.borderColor = self.textViewBodyBorderColor!
    self.textViewBody.layer.borderWidth = self.textViewBodyBorderWidth!

    print("self.textViewBody.layer.borderColor: \(self.textViewBody.layer.borderColor)\n\tself.textViewBody.layer.borderWidth: \(self.textViewBody.layer.borderWidth)")

}

调试窗口输出:

self.textViewBodyBorderColor: Optional(UIExtendedGrayColorSpace 0 1)
self.textViewBodyBorderWidth: 1.0
self.textViewBody.layer.borderColor: Optional(UIExtendedGrayColorSpace 0 1)
self.textViewBody.layer.borderWidth: 1.0

字面量赋值的代码及输出

当在viewWillAppear(_:)中使用字面量设置layer.borderColor和layer.borderWidth时,textViewBody的边框正常显示:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    print("self.textViewBodyBorderColor: \(self.textViewBodyBorderColor)\n\tself.textViewBodyBorderWidth: \(self.textViewBodyBorderWidth!)")

    self.textViewBody.layer.borderColor = UIColor.black.cgColor
    self.textViewBody.layer.borderWidth = 1.0

    print("self.textViewBody.layer.borderColor: \(self.textViewBody.layer.borderColor)\n\tself.textViewBody.layer.borderWidth: \(self.textViewBody.layer.borderWidth)")

}

调试窗口输出:

self.textViewBodyBorderColor: Optional(UIExtendedGrayColorSpace 0 1)
self.textViewBodyBorderWidth: 1.0
self.textViewBody.layer.borderColor: Optional(UIExtendedGrayColorSpace 0 1)
self.textViewBody.layer.borderWidth: 1.0

补充信息

初始时通过Identity Inspector的「User Defined Runtime Attributes」设置这些属性,Key Path分别为layer.borderColor(类型为Color,值为黑色)和layer.borderWidth(类型为Number,值为1)。


内容的提问来源于stack exchange,提问作者daniel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 11:35:21