0

I create a view called boundView using IB and Auto Layout,then in the controller I call [self.boundView layoutIfNeeded],then I pass self.boundView.frame.size to a method to generate the size of boundView's subview CardView. And then use the

 PlayingCardView *playingCardView = [[PlayingCardView alloc]initWithFrame:frame];

to create subview programmatically. I do use NSLog to check that the size of subview is smaller than superview. But when I use [self.boundView addSubview:CardView] to add the subview. It is larger than superview! Is there something wrong with the coordinate?Or it is because I combine the Auto Layout with the view I create by code?

3
  • you should not combine autolayout with static data Commented Mar 21, 2016 at 10:56
  • actually autolayout change frame at runtime... when you will boundView.frame.size then actually you are getting IB's frame of view... not frame which is set by autolayout Commented Mar 21, 2016 at 11:00
  • So that means I cannot get the correct frame of the view set by auto layout? I can only set the size using static frame? Commented Mar 21, 2016 at 11:06

1 Answer 1

1

Where are you doing this? If it is in viewDidLoad then the autolayout sizes will not have been calculated yet. Try doing it in viewDidLayoutSubviews.

Sign up to request clarification or add additional context in comments.

5 Comments

I have already tried it, and I also try to call layoutIfNeeded in my method.But it still does't work.I think that what @Divyanshu Shama said in comment is right. I always get IB's frame of view which is larger than my boundView.So I have to set the frame in code rather than using Auto Layout.
Well, you will not always get the IB size - that wouldn't make any sense. You just need to do it after the view has been laid out. However, I can't really see if this is in fact your issue without knowing more about your setup. Maybe you could provide more code. Also, tell us exactly where you do stuff, i.e. in what method rather then just saying "in the controller".
Well,Finally I find the issue.Just as what you said in the answer.I check my code again,and find that I call a method in viewDidLoad,and in this method I call layoutIfNeeded.As the auto layout size hasn't been calculated, I always get the wrong size.I try to fix it by calling layoutIfNeeded in ViewDidLoad,but the result is obviously same as above.Just now, I call the my method in viewDidLayoutSubviews and finally I get the correct frame.Thanks a lot!
You're welcome. And just to clarify for next time: What layoutIfNeeded does is to merely force the updating of the layout immediately in stead of waiting for the next update loop cycle. Also, it only has an effect if UIKit sees that the layout actually needs updating (you can tell it this by calling setNeedsLayout). In any case, you would still need to get into viewDidLayoutSubviews to get the newest frames calculated by the autolayout.
I get it. I misunderstood layoutIfNeeded before. Hopefully, I can continue my work now.

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.