简体   繁体   中英

Can't set frame of a UIView while it's a child of UIScrollView

I have created one UIView inside the UIScrollView from storyboard.

在此输入图像描述

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!

在此输入图像描述

my code: 在此输入图像描述

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

Most impornatant condition is Autolayout is "TRUE".

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, eg, 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM