IB_DESIGNABLE,在预览中显示视图?

Jul*_*les 5 xcode cocoa-touch interface-builder ios

我在界面构建器中使用我的IB_DESIGNABLE属性集显示了我的视图,但是当我使用预览功能查看我的视图在各种设备上的外观时,我只获得超级视图名称,例如UIView.

请勿IB_DESIGNABLE在预览中显示视图?

编辑按要求的代码+屏幕截图:

H
=
#import <UIKit/UIKit.h>
IB_DESIGNABLE
@interface TitleBannerView : UILabel
@property (nonatomic) IBInspectable CGFloat borderWidth;
@property (nonatomic) IBInspectable CGFloat cornerRadius;
@property (nonatomic) IBInspectable CGFloat shadowHeight;
@end

M
=
@implementation TitleBannerView
@synthesize borderWidth;
@synthesize cornerRadius;
@synthesize shadowHeight;

- (void)drawRect:(CGRect)rect {

    if (self.borderWidth == 0) self.borderWidth = 8.5;
    if (self.cornerRadius == 0) self.cornerRadius = 33;
    if (self.shadowHeight == 0) self.shadowHeight = 15.1;

    CGContextRef context = UIGraphicsGetCurrentContext();

    //// Color Declarations
    UIColor* bG = [UIColor colorWithRed: 0.18 green: 0.8 blue: 0.443 
       alpha: 1];
    UIColor* borderColor = [UIColor colorWithRed: 0.557 green: 0.267 blue: 0.678 
       alpha: 1];
    UIColor* shadowColor2 = [UIColor colorWithRed: 0.976 green: 0.859 blue: 0.718 
       alpha: 1];

    //// Shadow Declarations
    UIColor* shadow = shadowColor2;
    CGSize shadowOffset = CGSizeMake(-4.1, self.shadowHeight);
    CGFloat shadowBlurRadius = 1.5;
Run Code Online (Sandbox Code Playgroud)

//

    //// Frames
    CGRect frame = self.bounds;

    //// Subframes
    CGRect group = CGRectMake(CGRectGetMinX(frame) + 15, 
        CGRectGetMinY(frame) + 15, CGRectGetWidth(frame) - 28, 
        CGRectGetHeight(frame) - 40);


    //// Abstracted Attributes
    CGFloat roundedRectangleStrokeWidth = self.borderWidth ;
    CGFloat roundedRectangleCornerRadius = self.cornerRadius;

    //// Group
    {
        //// Rounded Rectangle Drawing
        UIBezierPath* roundedRectanglePath = [UIBezierPath bezierPathWithRoundedRect: 
           CGRectMake(CGRectGetMinX(group) + floor(CGRectGetWidth(group) * 0.00060) + 0.5, 
           CGRectGetMinY(group) + floor(CGRectGetHeight(group) * 0.00568) + 0.5, 
          floor(CGRectGetWidth(group) * 0.99940) - floor(CGRectGetWidth(group) * 0.00060), 
         floor(CGRectGetHeight(group) * 0.99432) - floor(CGRectGetHeight(group) * 0.00568)) 
         cornerRadius: roundedRectangleCornerRadius];
        CGContextSaveGState(context);
        CGContextSetShadowWithColor(context, shadowOffset, shadowBlurRadius, 
            shadow.CGColor);
        [bG setFill];
        [roundedRectanglePath fill];
        CGContextRestoreGState(context);

        [borderColor setStroke];
        roundedRectanglePath.lineWidth = roundedRectangleStrokeWidth;
        [roundedRectanglePath stroke];

    }
}
@end
Run Code Online (Sandbox Code Playgroud)

TitleBannerView是绿色的视图,带有紫色边框. 在此输入图像描述

Ist*_*van 1

您有一些模拟数据要在界面生成器中显示吗?

\n\n

当您使用@时,IBDesignable您的初始值设定项、布局和绘图方法将用于在画布上渲染您的自定义视图

\n\n

由于在 Interface Builder 中呈现时,自定义视图不会\xe2\x80\x99 拥有应用程序的完整上下文,因此您可能需要生成用于显示的模拟数据,例如默认用户个人资料图像或通用天气数据。有两种方法可以为这个特殊上下文添加代码

\n\n
    \n
  • prepareForInterfaceBuilder():此方法与其余代码一起编译,但仅当您的视图准备在 Interface Builder 中显示时才执行。
  • \n
  • TARGET_INTERFACE_BUILDER:#if TARGET_INTERFACE_BUILDER预处理器宏将在 Objective-C 或 Swift 中工作,以便根据情况有条件地编译正确的代码
  • \n
\n\n

您可能想看一下本教程和示例

\n