我发现将 setterIBDesignable
的IBInspectable
可能性直接带入情节提要非常有用。
我以这种方式在快速项目中使用它
import Foundation
import UIKit
@IBDesignable extension UIView {
@IBInspectable var addBorderTop: CGFloat {
set {
addBorderUtility(0, y: 0, width: frame.width, height: newValue, color: layer.borderColor)
}
get {
return 0
}
}
@IBInspectable var addBorderBottom: CGFloat {
set {
addBorderUtility(0, y: frame.height - newValue, width: frame.width, height: newValue, color: layer.borderColor)
}
get {
return 0
}
}
@IBInspectable var addBorderLeft: CGFloat {
set {
addBorderUtility(0, y: 0, width: newValue, height: frame.height, color: layer.borderColor)
}
get {
return 0
}
}
@IBInspectable var addBorderRight: CGFloat {
set {
addBorderUtility(frame.width - newValue, y: 0, width: newValue, height: frame.height, color: layer.borderColor)
}
get {
return 0
}
}
private func addBorderUtility(_ x: CGFloat, y: CGFloat, width: CGFloat, height: CGFloat, color: CGColor?) {
let border = CALayer()
border.backgroundColor = color
border.frame = CGRect(x: x, y: y, width: width, height: height)
layer.addSublayer(border)
}
}
这就像一个魅力。所以我决定在一个objective-c项目上做同样的事情,尝试在这里扩展UIView是它的样子
UIView+边框.h
#import <UIKit/UIKit.h>
@interface UIView ()
@property (nonatomic) IBInspectale CGFloat addBorderTop;
@property (nonatomic) IBInspectale CGFloat addBorderBottom;
@property (nonatomic) IBInspectale CGFloat addBorderLeft;
@property (nonatomic) IBInspectale CGFloat addBorderRight;
@end
UIView+边框.m
#import "UIView+Border.h"
@interface UIView ()
IB_DESIGNABLE
@implementation UIView ()
- (void) setAddBorderTop: (CGFloat) newValue
{
addBorderUtility(0, y: 0, width: frame.width, height: newValue, color: layer.borderColor)
}
- (void) setAddBorderBottom: (CGFloat) newValue
{
addBorderUtility(0, y: frame.height - newValue, width: frame.width, height: newValue, color: layer.borderColor)
}
- (void) setAddBorderLeft: (CGFloat) newValue
{
addBorderUtility(0, y: 0, width: newValue, height: frame.height, color: layer.borderColor)
}
- (void) setAddBorderRight: (CGFloat) newValue
{
addBorderUtility(frame.width - newValue, y: 0, width: newValue, height: frame.height, color: layer.borderColor)
}
- (void) addBorderUtility: (CGFloat):x y:(CGFloat)y width:(CGFloat)width height:(CGFloat)height color:(CGColor)color
{
CALayer *border = [CALayer init]
border.backgroundColor = color
border.frame = CGRect(x: x, y: y, width: width, height: height)
layer.addSublayer(border)
}
@end
但是我的自定义变量都不能从情节提要的右侧菜单中设置。我还尝试添加一个User Defined Runtime Attributes
与我的客户变量之一同名的变量,运行该应用程序后没有任何反应。
知道该怎么做吗?谢谢