Set a border for UIButton in Storyboard(在 Storyboard 中为 UIButton 设置边框)
问题描述
如果不直接在代码中设置它们,我无法在 Xcode 5 中为我的按钮设置边框.如果不制作自定义背景图像,是否有可能无法在情节提要上执行此操作?
I can't get a border onto my buttons in Xcode 5 without setting them directly in the code. Is it possible that there's no way to do this on the storyboard without making a custom background image?
推荐答案
可以使用key path.
You can use key path.
例如图片上描述的角半径(layer.cornerRadius).您将无法在情节提要上看到效果,因为此参数是在运行时评估的. 现在您可以在 @IBInspectable<中使用 UIView 中的 swift 类别(图片下方的代码)/code> 在情节提要上显示结果(如果您使用类别,请仅使用 cornerRadius 而不是 layer.cornerRadius 作为关键路径.
For example the corner radius (layer.cornerRadius) as describe on the image.
You will not be able to see the effects on storyboard, cause this parameters are evaluated at runtime. Now you can use a swift category in UIView (code bellow the picture) in with @IBInspectable to show the result at the storyboard (If you are using the category, use only cornerRadius and not layer.cornerRadius as a key path.
extension UIView {
@IBInspectable var cornerRadius: CGFloat {
get {
return layer.cornerRadius
}
set {
layer.cornerRadius = newValue
layer.masksToBounds = newValue > 0
}
}
}
<小时>
这是来自 Peter DeWeese 答案的类别,允许使用 keypath layer.borderUIColor 来设置边框颜色.
Here is category from Peter DeWeese answer that allow use keypath layer.borderUIColor to set the border color.
CALayer+XibConfiguration.h:
CALayer+XibConfiguration.h:
#import <QuartzCore/QuartzCore.h>
#import <UIKit/UIKit.h>
@interface CALayer(XibConfiguration)
// This assigns a CGColor to borderColor.
@property(nonatomic, assign) UIColor* borderUIColor;
@end
CALayer+XibConfiguration.m:
CALayer+XibConfiguration.m:
#import "CALayer+XibConfiguration.h"
@implementation CALayer(XibConfiguration)
-(void)setBorderUIColor:(UIColor*)color
{
self.borderColor = color.CGColor;
}
-(UIColor*)borderUIColor
{
return [UIColor colorWithCGColor:self.borderColor];
}
@end
这篇关于在 Storyboard 中为 UIButton 设置边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Storyboard 中为 UIButton 设置边框
基础教程推荐
- iOS - UINavigationController 添加多个正确的项目? 2022-01-01
- Xcode UIView.init(frame:) 只能在主线程中使用 2022-01-01
- Android Volley - 如何动画图像加载? 2022-01-01
- 如何将图像从一项活动发送到另一项活动? 2022-01-01
- SwiftUI-ScrollViewReader的ScrollTo不滚动 2022-01-01
- 为什么姜饼模拟器方向卡在应用程序中? 2022-01-01
- UIImage 在开始时不适合 UIScrollView 2022-01-01
- 如何比较两个 NSDate:哪个是最近的? 2022-01-01
- navigationItem.backBarButtonItem 不工作?为什么上一个菜单仍然显示为按钮? 2022-01-01
- Play 商店的设备兼容性问题 2022-01-01
