Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS converting UIView to ScrollView without destroying layout?

Is there any possibility to move from UIView to UIScrollView without destroying all the constraints and placement. The thing is I build the whole UI without testing it on iPhone 4 and now I see some views should be in Scroll View to work. I tried few tricks but nothing works. Constraints gets deleted. Below is the example picture of sample:

enter image description here

Now I want that test1 UIView to be Scroll View, I tried to put Scroll View inside test1 View and then recursively copy test1 inside scroll view and delete old elements but it didnt work (shown bellow)

enter image description here

If there is any way to convert UIView to UIScrollView without destroying constraints (even programatically) that would be great. Thanks

like image 356
gorgi93 Avatar asked Apr 06 '15 22:04

gorgi93


1 Answers

Yes

Simply replace the <view> element by <scrollView> in your XIB file:

  1. Make a backup copy of your XIB file.

  2. Edit your XIB file as plain text:

    • Xcode > Right click on XIB > Open As > Source Code
    • Finder > Right click on XIB > Open With > TextEdit
  3. Replace the specific <view> tag by <scrollView>.

  4. Replace attributes (see example below).

  5. Keep the original id attribute.

  6. Replace the closing </view> tag by </scrollView>.

I choose my battles. I prefer constraints as code.


Example, before:

<view contentMode="scaleToFill" 
 translatesAutoresizingMaskIntoConstraints="NO"
 id="ABC-XY-123">
...
</view>

After:

<scrollView 
 clipsSubviews="YES" 
 multipleTouchEnabled="YES" 
 contentMode="scaleToFill" 
 alwaysBounceVertical="YES" 
 translatesAutoresizingMaskIntoConstraints="NO" 
 id="ABC-XY-123">
...
</scrollView>
like image 51
rjobidon Avatar answered Oct 20 '22 00:10

rjobidon