Unable to insert UIScrollView above UIView to another scene using XCode 8

大兔子大兔子 提交于 2019-12-22 08:28:47

问题


I have an existing iPad app (Portrait mode) where I am trying to add Landscape mode in XCode. I have been able to do it on the first simple view, seen below by changing the class in XCode to UIScrollView and the UIView fell into line below it (I believe the UIView was added by XCode, but not sure tho'). The scrollview works perfectly when in landscape mode.

When I try to do the same thing to the next scene, it won't allow me to change the order again, as seen in the following image; I'm not sure why it won't allow the same thing:

Any ideas? Is there another way to accomplish this?

UPDATE Here is the image showing the position of attempting to insert a UIScrollView in the UIVIewController (Book Details is the UIViewController; Book Detail View is the UIView):


回答1:


Well do this on second controller: 1. Delete the main UIView from UIViewController scene then, 2. Add a UIScrollView on top of it. 3. Add the UIView as UIScrollView subview.

You will have the perfect order and the desired result like in the first scene. In your case select the View under Bottom layout Guide, press backspace, drag and drop a UIScrollView on that Book Details. Cheers.

This is the result:




回答2:


HOW TO DEAL WITH SCROLLVIEW

  1. Your navigation bar is always be at the top with height 44px and top space of 20px ( top 20px is for the status bar i.e. charging, network etc icon you see in your device ).

Reason behind to fix the navigation bar at top is that wherever user is in the VC you always have to show him in which VC he is present at that time otherwise your app quality may be get low(i said may be). After all you can add navigation Bar in UIScrollView its your choice.

Top green view you are seeing, I just taking a view as a statusBar of height 20px to show the difference.

  1. Now add UIScrollView in the rest of part of the ViewController and give below constraints:

    • top to navgationBar 0px
    • Leading, Trailing, bottom to superview as 0.

Now your VC be like below. Red point indicating that you can constraint missing. Don't worry about that until your components inside the UIScrollView not set properly it shows you same error.

  1. Now add a UIView in your UIScrollView (says viewMain) and give constraints like this

    • top, bottom, leading, trailing to scrollview as 0
    • Equal width to you viewVC. viewVC is the view of VC. you get this view when you take new UIViewController. Now your UI will be like

and your view hierarchy will be like

here I am assuming you just scrolling your view in vertical direction not in horizontal direction.

  1. Now I am going to add 5 views of different height in scrollview check it out.

  2. Add a view in viewMain(says viewYellow) and give constraint below;

    • top, leading trailing to mainView as 0px
    • height 180px

Now your UI will be like

  1. Now same as step 4 add four more views (viewGreen, viewBlue, viewRed, viewPurple) and gives constraints like below

      • viewGreen : 1. top to viewYellow 0px 2. leading trailing to mainView as 0pxand 3. height 150px.
      • viewBlue : 1. top to viewGreen 0px 2. leading trailing to mainView as 0pxand 3. height 80px.
      • viewBlue : 1. top to viewGreen 0px 2. leading trailing to mainView as 0pxand 3. height 80px.
      • viewRed : 1. top to viewBlue 0px 2. leading trailing to mainView as 0pxand 3. height 140px.
      • viewPurple : 1. top to viewRed 0px 2. leading trailing to mainView as 0pxand 3. height 128px. 4. bottom to mainView 0px.

Now your UI and view hierarchy will be like below

Don't worry about viewPurple as it is little appear in the UI but available in hierarchy. it is scrollView and it is safe very much. See

Note: There is not constraints error and warning available in UIViewController because as I ready said you when you set internal component of scrollView properly with constraints constraints error will be solved.

Important: How ScrollView Height is Calculated

As you set constraint of each view with top, bottom and height constraint to the mainView so mainView height is calculated as follow equation

mainView.height = viewYellow.top + viewYellow.height + .... for all view + viewPurple.bottom

and scrollView height will be calculated as follows:

scrollView.height = mainView.height

However every view have constant height so on rotation height will be same.

Final Output

portrait

Landscape




回答3:


None of the answers solved the issue of altering an existing app, so I requested Apple Technical Support. They didn't solve it either, but rather gave me some docs to look at. @Aragunz probably came the closest by suggesting it was/is a problem within XCode 8. My only option left is to rewrite the storyboard from scratch, saving the existing classes.




回答4:


Select the Book Details UIViewController in your storyboard and then just select the root view. Delete it and then drag and drop a UIScrollView to your UIViewController. Your root view should now be a UIScrollView

Note: I personally prefer the following view hierarchy:

|---UIView
     |---UIScrollView
         |---UIView //(to act as a container)
              |---UIView
              |---UIView
              |---UIView
              |---UIView

Allows you to scale the application as required quite easily while also allowing autoLayout to quite easily manage setting the content size of your UIScrollView



来源:https://stackoverflow.com/questions/41964016/unable-to-insert-uiscrollview-above-uiview-to-another-scene-using-xcode-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!