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 设置边框
基础教程推荐
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01