问题描述
如果不直接在代码中设置它们,我无法在 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 设置边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!