ios - 使用 IB_DESIGNABLE obj-c 扩展类

标签 ios objective-c storyboard ibdesignable ibinspectable

我发现 IBDesignableIBInspectable 非常有用,可以将 setter 可能性直接引入 Storyboard。

我以这种方式在快速项目中使用它

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+Border.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+Border.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

但是我的自定义变量都无法从 Storyboard 上的右侧菜单进行设置。我还尝试添加一个与我的客户变量同名的用户定义的运行时属性,但运行应用程序后什么也没有发生。

知道该怎么做吗?谢谢

最佳答案

你的Objective-C代码中有很多错误,我已经更新了。 您必须使用 CALayer *border = [CALayer layer]; 而不是 CALayer *border = [CALayer init];

请检查此代码:

UIView+Border.h

#import <UIKit/UIKit.h>

@interface UIView (Border)
@property (nonatomic) IBInspectable CGFloat addBorderTop;
@property (nonatomic) IBInspectable CGFloat addBorderBottom;
@property (nonatomic) IBInspectable CGFloat addBorderLeft;
@property (nonatomic) IBInspectable CGFloat addBorderRight;

@end

UIView+Border.m

#import "UIView+Border.h"

IB_DESIGNABLE
@implementation UIView (Border)

@dynamic addBorderTop;
@dynamic addBorderBottom;
@dynamic addBorderLeft;
@dynamic addBorderRight;

- (void) setAddBorderTop: (CGFloat)newValue
{
    [self addBorderUtility:0 y:0 width:self.frame.size.width height:newValue color:self.layer.borderColor];
}

- (void) setAddBorderBottom: (CGFloat) newValue
{
    [self addBorderUtility:0 y:self.frame.size.height - newValue width:self.frame.size.width height:newValue color:self.layer.borderColor];
}

- (void) setAddBorderLeft: (CGFloat) newValue
{
    [self addBorderUtility:0 y:0 width:newValue height:self.frame.size.height color:self.layer.borderColor];
}

- (void) setAddBorderRight: (CGFloat) newValue
{
    [self addBorderUtility:self.frame.size.width - newValue y:0 width:newValue height:self.frame.size.height color:self.layer.borderColor];
}

- (void) addBorderUtility:(CGFloat)x y:(CGFloat)y width:(CGFloat)width  height:(CGFloat)height color:(CGColorRef)color
{
    CALayer *border = [CALayer layer];
    border.backgroundColor = color;
    border.frame = CGRectMake(x, y, width, height);
    [self.layer addSublayer:border];
}
@end

关于ios - 使用 IB_DESIGNABLE obj-c 扩展类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48621521/

相关文章:

ios - 如何仅在 Interface Builder 中使用键盘移动控件

ios - OS X 服务器机器人的文件存储在哪里

ios - 从UICellView中的UIButton调用函数

iOS 为 iPhone 4 和 5 制作通用应用程序

swift - NSLayout 属性从左到右改变图像的位置

ios - 离开/关闭按钮操作上的文本字段

ios - 如何检索 Xcode 中丢弃的更改?

objective-c - 从 NSUserDefaults 中的数组中删除对象

iphone - 是否可以使一个方法顺序运行?

macos - 如何从 Storyboard加载初始窗口 Controller ?