2015-11-02 96 views
4

我使用我的IB_DESIGNABLE屬性集在界面生成器中顯示了我的視圖,但是當我使用預覽功能查看我的視圖在各種設備上的外觀時,我只是獲取超級視圖名稱,例如UIViewIB_DESIGNABLE,在預覽中顯示視圖?

不要IB_DESIGNABLE views在預覽中顯示?

編輯爲要求代碼+截屏:

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 

的TitleBannerView是綠色的觀點與紫色的邊界。 enter image description here

+0

你的意思是你用@IBDesignable來代替指定視圖的屬性你的看法? – wj2061

+0

不,我沒有看到我在預覽中創建的圖紙。 – Jules

+0

它在模擬器上工作嗎? – wj2061

回答

0

你有一些模擬數據接口生成器顯示?

當您使用@IBDesignable你的初始化,將用於佈置和繪製方法來呈現在畫布上

由於自定義視圖上的自定義視圖就在渲染時不會有你的應用程序的完整的上下文界面生成器,您可能需要生成模擬數據以進行顯示,例如默認用戶配置文件圖像或通用天氣數據。有兩種方法可以添加代碼爲這個特殊的背景下

  • prepareForInterfaceBuilder():此方法與你的代碼的其餘部分編譯,但是當被用於在Interface Builder顯示準備好視圖時,纔會執行。
  • TARGET_INTERFACE_BUILDER:本#if TARGET_INTERFACE_BUILDER預處理器宏會在任何的Objective-C或斯威夫特工作條件編譯權代碼的情況

你可能想利用這個tutorial和例子來看看

+0

在我的直接方法中,我檢查零值,並指定一些默認值 – Jules

+0

@Istvsn應該是好的,不是嗎? – Jules

+0

什麼值?你也設置了一些顏色?你想在畫布上看到什麼?發佈一些代碼請 – Istvan

3

第一,IBDesignable可以在預覽中看到。

您是否在框架中聲明IBDesignable類? IBInspectable只在框架中聲明時才支持預覽。

而且,記住(不是你的情況):

  1. 您的抽獎代碼應該足夠快,你只需要100毫秒的時間繪製一個項目。

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

+0

我目前沒有我的繪圖類在框架中我會嘗試並回發 – Jules

+0

如果您真的不要,它應該是最可能的原因。祝你好運。 – MatthewLuiHK

+0

剛剛測試過它,它確實渲染。我怎麼錯過了這個?你是如何發現它的? :) –

0

IBDesignable作品只有在Interface Builder,但不是在預覽:-( 我希望蘋果修復它在新的Xcode ...

+0

你有來源嗎? – Jules

+0

嗡嗡聲,我的所有自定義控件都在界面生成器中正確顯示,但不在預覽 –

+0

@MatthewLuiHK也許答案的作品,我還沒有檢查 –