Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get dimensions of a custom UIView added via storyboard in initWithCoder:?

I have a custom UIView subclass that adds a subview programatically during initialisation. I'm wanting this subview to be the same width as the custom UIView.

My code works fine if I programatically add the custom UIView. However, when the UIView is initialised via the StoryBoard, I cannot obtain the custom UIView's width.

// Init programatically
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        NSLog (@"view: %f", self.frame.size.width);   //WILL return width
        NSLog (@"view: %f", self.bounds.size.width);  //WILL return width
    }
    return self;
}

// Init from storyboard
- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {

        NSLog (@"view: %f", self.frame.size.width);   //Returns 0
        NSLog (@"view: %f", self.bounds.size.width);  //Returns 0
    }
    return self;
}

When initWithCoder: is run by StoryBoard, the UIView's frame and bounds are all returned as 0. Is there any way of accessing the UIView's dimensions within initWithCoder?? Or, at least access the dimensions before drawRect: is called?

Thanks.

like image 681
So Over It Avatar asked Mar 02 '13 04:03

So Over It


People also ask

How to modify storyboard UI component’s attribute in Swift?

Swift use @IBOutlet to specify a class property as an outlet variable, then you can use this class property to modify storyboard UI component’s attribute. Below example define a UILabel type variable, @IBOutlet means this variable is an outlet variable which can modify related label in the storyboard ui.

How to connect storyboard UI components to viewcontroller?

First you should add connections between storyboard UI components and ViewController class source code. The connection has a lot of types such as outlet (a class property variable that is used to refer to UI component), action (a class method that is used to response action event which UI component triggered.)

Why does UIView have an init in it?

UIView has such an init because it conforms to NSCoding, a protocol for the view to be encoded and decoded for archiving. Our custom view has to implement (it is an override, but without the override modifier), and decode to init the view.

What is a custom view in Swift?

Custom view will be composed of other views, with certain custom behaviours. There are often times when you construct your storyboard or init your view controller with multiple views, but they could – alternatively – be accomplished with a custom view. It is important to learn the fundamental about Swift initialization.


2 Answers

Rather than set the subviews dimensions in the init function, set it relative to the custom view in the layoutSubviews method of the custom view. Doing it here will mean that the width of the subview will also always change to match the width of the custom view even if you change the dimensions of the custom view at any point after initialisation.

like image 153
Brett Avatar answered Oct 26 '22 23:10

Brett


You can re-use your standard initWithFrame call.

- (id)initWithCoder:(NSCoder *)aDecoder 
{
    self = [super initWithCoder:aDecoder];
    if (self) {

        [self initWithFrame:[self frame]];

        NSLog (@"view: %f", self.frame.size.width);   
        NSLog (@"view: %f", self.bounds.size.width); 
    }
    return self;
}
like image 24
iPatel Avatar answered Oct 27 '22 01:10

iPatel