问题
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
- Your
navigation bar
is always be at the top with height44px
and top space of20px
( top20px
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.
Now add
UIScrollView
in the rest of part of theViewController
and give below constraints:- top to navgationBar
0px
- Leading, Trailing, bottom to superview as
0
.
- top to navgationBar
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.
Now add a
UIView
in yourUIScrollView
(says viewMain) and give constraints like this- top, bottom, leading, trailing to
scrollview
as0
- Equal width to you
viewVC
.viewVC
is the view of VC. you get this view when you take newUIViewController
. Now your UI will be like
- top, bottom, leading, trailing to
and your view hierarchy will be like
here I am assuming you just scrolling your view in vertical direction not in horizontal direction.
Now I am going to add 5 views of different height in
scrollview
check it out.Add a view in
viewMain
(says viewYellow) and give constraint below;- top, leading trailing to
mainView
as0px
- height
180px
- top, leading trailing to
Now your UI will be like
Now same as
step 4
add four more views (viewGreen, viewBlue, viewRed, viewPurple) and gives constraints like belowviewGreen :
1. top toviewYellow
0px
2. leading trailing tomainView
as0px
and 3. height150px
.
viewBlue :
1. top toviewGreen
0px
2. leading trailing tomainView
as0px
and 3. height80px
.
viewBlue :
1. top toviewGreen
0px
2. leading trailing tomainView
as0px
and 3. height80px
.
viewRed :
1. top toviewBlue
0px
2. leading trailing tomainView
as0px
and 3. height140px
.
viewPurple :
1. top toviewRed
0px
2. leading trailing tomainView
as0px
and 3. height128px
. 4. bottom tomainView
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