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 设置边框
基础教程推荐
- Android ViewPager:在 ViewPager 中更新屏幕外但缓存的片段 2022-01-01
- “让"到底是怎么回事?关键字在 Swift 中的作用? 2022-01-01
- 如何使 UINavigationBar 背景透明? 2022-01-01
- :hover 状态不会在 iOS 上结束 2022-01-01
- 使用 Ryzen 处理器同时运行 WSL2 和 Android Studio 2022-01-01
- 固定小数的Android Money Input 2022-01-01
- 如何使用 YouTube API V3? 2022-01-01
- Android文本颜色不会改变颜色 2022-01-01
- 在 iOS 上默认是 char 签名还是 unsigned? 2022-01-01
- LocationClient 与 LocationManager 2022-01-01
