【问题标题】:extend class with IB_DESIGNABLE obj-c使用 IB_DESIGNABLE obj-c 扩展类
【发布时间】:2018-07-15 05:56:05
【问题描述】:

我发现IBDesignableIBInspectable 非常有用,可以将设置器的可能性直接带入情节提要。

我在 swift 项目中这样使用它

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

但是我的自定义变量都不能从情节提要的右侧菜单中设置。我还尝试添加一个与我的客户变量之一同名的User Defined Runtime Attributes,一旦您运行该应用程序就没有发生任何事情。

知道怎么做吗?谢谢

【问题讨论】:

  • 你在这里打错了IBInspectaleIBInspectable
  • 也应该是@property (nonatomic) IBInspectable CGFloat addBorderTop;,没有*
  • 谢谢我更新问题。
  • 请检查我的更新答案。

标签: ios objective-c storyboard ibdesignable ibinspectable


【解决方案1】:

你的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

【讨论】:

  • 我确实按照您的指示进行了操作,但从属性检查器中看不到任何更改。 suprinsigly Xcode 从不显示我的老错误
  • 哎呀,又打错了。现在一切正常,谢谢。但是您知道为什么 xcode 没有从这些文件中显示拼写错误吗?无论如何,谢谢。
【解决方案2】:

您没有正确定义类别。您需要指定类别名称,否则编译器会将其解释为 UIView 的重新声明。试试@interface UIView (Design)@implementation UIView (Design)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-14
    • 2016-08-02
    • 2015-06-18
    相关资源
    最近更新 更多