9

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

3
  • 1
    Have you tried right click the view, go to editor and embed in scrollView? Commented Apr 6, 2015 at 22:33
  • I cant embed test1 in scroll view since it is root view. Good idea though. Is there trick to do that with root view? Commented Apr 6, 2015 at 22:37
  • Try copy and paste. Create a new controller, and add a scroll view to it's main view. Paste the view you copied from your other controller into the scroll view. Since you're making a copy, if it doesn't work, you'll still have the original. Commented Apr 6, 2015 at 23:05

1 Answer 1

2

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>
Sign up to request clarification or add additional context in comments.

Comments

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.