0

I am using storyboard in my project. Storyboard has a custom view with auto layout.

CustomView.h

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

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

If i run the app, it shows wrong frame.

Help me to solve this

9
  • What do you mean by "wrong"? What did you expect and what did you get? Commented Jun 11, 2015 at 10:54
  • @justMartin, if I run in iPhone6 then it should return 375 instead of 320. Commented Jun 11, 2015 at 10:56
  • Wrong compared to what? Do you mean that your View looks different in Interface Builder and on actual device/simulator? Commented Jun 11, 2015 at 10:57
  • @NKorotkov, if I run in iPhone6 then it should return 375 instead of 320 Commented Jun 11, 2015 at 10:58
  • 1
    Given that a view's frame is dependent on the views it is a subview of and the constraints setup across the entire view hierarchy, the only safe place to acquire frame sizes is from viewWillAppear or later once full layout has happened. Commented Jun 11, 2015 at 11:22

2 Answers 2

2

Because in initWithCoder,the constraints has not been applied to the view.

Example:

Code

-(void)viewDidAppear:(BOOL)animated{
    NSLog (@"viewDidAppear: %f", self.testview.frame.size.width);
}
-(void)viewDidLayoutSubviews{
    NSLog (@"viewDidLayoutSubviews: %f", self.testview.frame.size.width);
}
-(void)awakeFromNib{
    NSLog (@"awakeFromNib: %f", self.testview.frame.size.width);
}
-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super initWithCoder:aDecoder]) {
        NSLog (@"initWithCoder: %f", self.testview.frame.size.width);
    }
    return self;
}

And log

2015-06-11 19:09:27.647 OCTest[717:20692] initWithCoder: 0.000000
2015-06-11 19:09:27.649 OCTest[717:20692] awakeFromNib: 0.000000
2015-06-11 19:09:27.696 OCTest[717:20692] viewDidLayoutSubviews: 320.000000
2015-06-11 19:09:27.746 OCTest[717:20692] viewDidAppear: 320.000000
Sign up to request clarification or add additional context in comments.

4 Comments

I have use custom view i.e. subclass of UIVIew & viewDidLayoutSubviews not called in it.
I know.Just from the aspect of UIViewController to tell you that in initWithCoder,it is too early to get the frame.
When the super view layout subview,the constraints is applied. Only after that,you can get the right frame
So, what to do with custom view loads from storyboard directly
-1

I met the same problem, if you want to take the size of screen in InitWithCoder method - take it this way

CGFloat x = [UIScreen mainScreen].bounds.size.width;
CGFloat y = [UIScreen mainScreen].bounds.size.height;

Not from the still-not-existing bounds of the UIView, but out of the UIScreen.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.