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.
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.
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.)
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.
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.
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.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With