1
votes

I have created one UIView inside the UIScrollView from storyboard.

enter image description here

I want to set the frame to UIView programatically. I have tried to set frame using setFrame function programatically, but its not working for the UIView which is inside UIScrollView.

if I create one UIView inside the UIScrollView using [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 110)],and than change size of the view,it is work!

enter image description here

my code: enter image description here

but I want to create a uiview by IB and set frame using setFrame function.

Most impornatant condition is Autolayout is "TRUE".

1

1 Answers

3
votes

Most impornatant condition is Autolayout is "TRUE".

If you want to mix constraints with setting a view's frame directly, you will need to make sure to set view.translatesAutoresizingMaskIntoConstraints = YES for your view. AFAIK, this cannot be done in IB for iOS, only in code.

Also beware that setting translatesAutoresizingMaskIntoConstraints = YES will add new constraints to those you define in IB (or programmatically) and you have to ensure they remain consistent, otherwise you will get an exception. So, e.g., if you set the frame origin in code, but also pinned the top distance between your view and the superview, you will run into problems if those specifications are not compatible.