Subclass subcategory UIView Live Xcode Interface Builder (IB_DESIGNABLE) without drawRect:

Is it possible to create a subclass UIViewthat displays live in Xcode (by adding an attribute IB_DESIGNABLEas described here ), but does not have a custom method drawRect:?

We have a custom subclass UIViewthat uses some CAShapeLayerthat are added to self.layerfor drawing (hence, there is no need to override drawRect:). This class works fine in the application, but will not display on Xcode.

If we replicate the code in drawRect:, it works, but we would prefer that the drawing runs automatically on the layers.

Can this be done?


I also tried to do

- (void)drawRect:(CGRect)rect
{
    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    CGContextMoveToPoint(currentContext, self.myLayer1.frame.origin.x, self.myLayer1.frame.origin.y);
    [self.myLayer1 renderInContext:currentContext];
    CGContextMoveToPoint(currentContext, self.myLayer1.frame.origin.x, self.myLayer1.frame.origin.y);
    [self.myLayer2 renderInContext:currentContext];    
}

which seems to work on the device, but not on Xcode IB.

+4
3

UIView IB ( IB_DESIGNABLE) , drawRect: .
XCode 6.1 OEProgressIndicator xib. ( Editor/Debug Selected View), commonProgressIndicatorInit.

: , commonProgressIndicatorInit ( initWithFrame:(CGRect)frame), frame CGRectZero (x:0 y:0 width:0 height:0), (0, 0), -1.
, , , , , IB.

, layoutSubviews ( UIView), . , frame CGRectZero , Interface Builder.

+3

- (void)prepareForInterfaceBuilder, IB, .

: Live View

, , , , Objective-C.

drawRect, - (void)layoutSubviews, . , - (void)layoutSubviews , , .. (, - (void)awakeFromNib, Live Rendering, , - (void)prepareForInterfaceBuilder.

+2

, , , , , IBInspectable/IBDesignable, - . , ( ).

:

@IBDesignable
class MyView : UIView {

    @IBInspectable var cornerRadius:CGFloat {
        get { return self.layer.cornerRadius }
        set { self.layer.cornerRadius = newValue    }
    }

    @IBInspectable var borderWidth:CGFloat {
        get { return self.layer.borderWidth }
        set { self.layer.borderWidth = newValue }
    }

    @IBInspectable var borderColor:UIColor {
        get { return UIColor(CGColor: self.layer.borderColor) }
        set { self.layer.borderColor = newValue.CGColor }
    }
}

, , . .

, , WWDC §411 22- .

, , , , .

0
source

All Articles