IB_DESIGNABLE,是否在预览中显示视图? [英] IB_DESIGNABLE, having views show in preview?

查看:53
本文介绍了IB_DESIGNABLE,是否在预览中显示视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



不要在 IB_DESIGNABLE 视图中显示



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

  H 
=
#import< UIKit / UIKit.h>
IB_DESIGNABLE
@interface TitleBannerView:UILabel
@属性(非原子)IBInspectable CGFloat borderWidth;
@property(非原子)IBInspectable CGFloat cornerRadius;
@property(非原子)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();

////颜色声明
UIColor * bG = [UIColor colorWithRed:0.18绿色:0.8蓝色:0.443
alpha:1];
UIColor * borderColor = [UIColor colorWithRed:0.557绿色:0.267蓝色:0.678
alpha:1];
UIColor * shadowColor2 = [UIColor colorWithRed:0.976绿色:0.859蓝色:0.718
alpha:1];

////阴影声明
UIColor * shadow = shadowColor2;
CGSize shadowOffset = CGSizeMake(-4.1,self.shadowHeight);
CGFloat shadowBlurRadius = 1.5;

//

  ////框架
CGRect框架= self.bounds;

/////子帧
CGRect组= CGRectMake(CGRectGetMinX(帧)+ 15,
CGRectGetMinY(帧)+ 15,CGRectGetWidth(帧)-28,
CGRectGetHeight(frame)-40);


////抽象属性
CGFloat roundedRectangleStrokeWidth = self.borderWidth;
CGFloat roundedRectangleCornerRadius = self.cornerRadius;

/////组
{
//////圆角矩形绘制
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)-地板(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笔触];

}
}
@end

TitleBannerView是带有紫色边框的绿色视图。

解决方案

首先,IBDesignable可以在预览中看到。 / p>

您是否在框架中声明了IBDesignable类? IBInspectable仅在框架中声明时支持预览。



另外,请记住(不在您的情况下):


  1. 您的绘制代码应该足够快,您只有100ms的绘制时间。


  2. prepareForInterfaceBuilder()方法是经常需要的。



I have my views showing in interface builder with my IB_DESIGNABLE properties set, however when I use the preview feature to see how my views look on various devices, I just get the super view name, e.g. UIView.

Don't IB_DESIGNABLE views show in preview?

EDIT As requested code + screen shot:

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;

//

    //// 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

The TitleBannerView is the view in green with purple border.

解决方案

First, IBDesignable can be visible in preview.

Do you declare your IBDesignable class in a framework? IBInspectable ONLY supports preview when it's declared in a framework.

Also, keep in mind (not in your case):

  1. Your draw code should be fast enough, you only have 100ms draw time for an item.

  2. prepareForInterfaceBuilder() method is often needed.

这篇关于IB_DESIGNABLE,是否在预览中显示视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆