【发布时间】:2026-01-06 01:15:01
【问题描述】:
我正在尝试为 UIButton 保留边框。如果我使用以下代码,它就可以工作。
案例:1
[[_myButton layer] setBorderWidth:1.0f];
[[_myButton layer] setBorderColor:[UIColor lightGrayColor].CGColor];
但在我写之前:
self.baseTypeButton.layer.borderWidth=2.0f;
self.myButton.layer.borderColor=[UIColor lightGrayColor];
XCode 建议我做
现在我的代码改变了,但在这种情况下我没有设置边框:
案例:2
_myButton.layer.borderWidth=2.0f;
_myButton.layer.borderColor=(__bridge CGColorRef _Nullable)([UIColor lightGrayColor]);
我没有使用自动布局。任何人都可以解释case-1和case-2之间的区别。为什么 case-2 不起作用。
【问题讨论】:
-
你添加了
#import <QuartzCore/QuartzCore.h>框架吗? -
@ivarun:QuartzCore 框架已添加到我的项目中
-
UIColor 不是 CGColor。你不能桥接它。你可以用它来节省你的时间github.com/AlexHsieh/ButtonAppearance
-
@AlexHsieh:非常感谢您的支持。在您的 github 链接中缺少“AHButton.h”文件。
-
@uday.m ,有两种使用方式。 1. 使用 cocoapod 或 2. 你可以从这里复制文件github.com/AlexHsieh/ButtonAppearance/tree/master/Pod/Classes
标签: ios objective-c uibutton cgcolor