Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I resize a UIView and force its subviews to resize

I have a UIScrollView with a UIView as a subview. The UIView has a bunch of data entry fields arranged vertically - essentially just a fixed format data entry Form.

I want to keep the UIView's vertical size and adjust its horizontal size to match the size of the UIScrollView which changes depending on the orientation of the device. Note that this is all placed in the Detail view of a UISplitViewController.

So the user will have to scroll vertically but not horizontally as all the text fields on the UIView should resize themselves to fit horizontally on the screen.

Currently if I resize the UIView by changing the frame width to match the UIScrollView's frame width then the UIView subviews (the text fields) don't resize themselves according to the constraints setup in IB. The UIView just seems to get clipped. There is no horizontal scrolling so this aspect is working correctly.

I have autoresize subviews set on UIView and on UIScrollView.

Any tips on what to do here ? Also where would I put code to resize the UIView if the device orientation changes ?

Additional information.

I created the UIView in IB as a separate view in the same NIB as the DetailViewController containing the UIScrollView. Because it is much taller than the UIScrollView the only way I can find for creating it in IB is if I set it up as a separate view of the desired width and height. I then create an IBOutlet and add this view as a subview to UIScrollView in the viewDidLoad method. This all seems to work find with the views all displaying correctly, with the exception that the UIView subviews are not resized horizontally.

Any suggestions on what I may be doing wrong here?

like image 847
Duncan Groenewald Avatar asked Sep 04 '25 17:09

Duncan Groenewald


1 Answers

Since you are using not putting the view inside scrollview directly from the xib, the IB doesn't provide the options to give constraints that has anything to do with superview. You might have to add the constraints programatically.See here.

EDIT:

Also try using the below on the view (haven't tried, should work according to documentation, but not sure with auto-layout):

self.view.autoresizesSubviews = YES;

Or if you don't want to use auto layout at all then the earlier method of setting the view to expand (horizontal/vertical) in the size inspector would do. For this you have to disable auto-layout. Select xib-> File Inspector -> Uncheck auto-layout checkbox

like image 52
Rakesh Avatar answered Sep 07 '25 17:09

Rakesh