I've many troubles with a ScrollView that is more little than main view.
My goal is the layout in the figure
where gray view is a TableView and blue is a hierarchy composed by a View inside the ScrollView. Everything is plaed all inside a main View. So the general hierarchy is the follow
In the ScrollView i've to put many elements, Switch for the final product, Label to understand. So i want the possibility to scroll.
Reading around the web i've found always the same, more or less, solution, this: one, two, three, four, five, etc.
As soon as i add various constraints as explained (for me are not all 0), both on the emulator and on the real device, the ScrollView disappears.
But as we see, in these example there is the case of a single ScrollView inside a main View, on full display. So i've tried also with another View to contain the hierarchy ScrollView-View. Unfortunately, i've the same results.
What is the problem? Where wrong i?
Control-drag from the content view of the scrollView to the viewControllers view and make Equal widths then change that constraint multiplier to 0.5 , same for the tableView sure minue margin between it and the scrollview
also make sure constraints are properly hooked from top to bottom
Here is a demo of what you want exactly scrollViewHalf
Related
I am quite new in swift programming and I am developing an app which has multiple custom views, hope some one can help me here.
The issue is that I have a scrollView, inside I have a stackView with about 20 Views inside, every view has inferred constraints and every view has a fixed height, maybe not the best practice but most of the views show correctly (had to re-do about 3 times to get here), but one of my views (around the middle of the stack) is showing correctly in my story board, but somehow it's shrinking it's subviews when launching the app, not sure if I am missing something, I have even removed all the content of these subviews and left only the main views with their heights but still I have the same issue.
To be more detailed the section that fails is a view inside the vertical stackView, it contains a view (container) which has the same leading, trailing, top and bottom as the first one. This container view, contains 5 (sub)views which have the same height each (124), and have practically the same content: a label, a texfield and a custom slider (thought the slider was ruining everything because it's custom but after removing all from the design the error still occurs).
Here is an image of how it's shown in the story board:
views in story board
And here is how it's displayed in the emulator:
view in emulator
Does anybody now why can this be happening? I am not doing any layout stuff in the code only a couple of corner rounding in other views that are inside the stack view.
BTW, I also implemented this 5 slider views using a vertical stack and had the same issue, I switched and changed to simple views and separated by 10 each bottom with the next view top.
Thanks
I’ve been struggling with this for a few days now…I can’t figure out how to correctly layout these constraints…. the bottom container view on the left is a UIPageViewController that loads the VC on the right…. that VC has a background view and 2 stackViews inside it… any ideas on how best to layout? I've tried constraining the leading and top of the background view to the safe area, then the same of the stackview (containing other stackviews) to the background view, but no matter what the stackviews display over each other?
The problem wasn't with auto-layout or my constraints, I was simply trying to fit too big of a view (the graph) inside it's superviews...when I shrunk the size of the graph, the existing constraints laid out the views perfectly.
To anyone else facing a similar issue however, Honey's comment is good advice, generally you should add the furthest "back" view first, add it's constraints, test to make sure it's correct, then repeat on each subview you add, as opposed to dragging all your views, then constraining them. The former is always the better approach. 👍
So I'm trying to get a layout to work using Scroll View.
Correct me if I'm wrong but from what I've been reading around the internet, it looks like to properly use Scroll Views and make it work with AutoLayout you need to have your root view, then put the scroll view inside it with constraints binding it to take the whole size of the root view (left/right/top/bottom constraints to 0) and then adding a view inside the scroll view and once again binding it to take up the whole space.
Afterwards, any ui elements or subviews would go in the innermost view.
My problem is that often I have experienced issues while settings my constraints within the scroll view where for example setting a trailing constraint to 0 wouldn't actually set to the end and so on, the numbers just wouldn't add up. In this case when I'm trying to make the innermost view take the whole space,it ends up messing it up more than anything; see image below.
Any help would be appreciated.
I just created this layout using the follow:
To test it, I added an image view to the content view and put a humongous image in it. It scrolls around quite nicely including bounce etc.
Update: You need to select your ViewController and uptick "Adjust Scroll View Insets"
Hope this helps.
I'm using Xcode 6.4 and I'm struggling with a large page with a bunch of various labels and text fields etc., that has to be scrollable. I looked at various tutorials some of which are obviously outdated and I had all my elements at first in a view that is inside of a scroll view. But that was a big pain with getting all the elements aligned properly using Auto Layout, in addition to not getting it to scroll either.
Now I see one person recommend using the following directly on the elements, without having a content view at all just a scroll view:
Editor -> Embed In -> Scroll View
I like it for the fact that nothing seems out of proportion, all the elements are shown on the iPhone exactly as I have it on the Storyboard without having to set any constraints, however I'm not able to get it to scroll.
This is my class
class ResNotFoundViewController: UIViewController, UIScrollViewDelegate
and I also try to set the height of the scrollview in the code as so:
#IBOutlet weak var scrollviewoutlet: UIScrollView!
and then this in the viewDidLoad as so:
scrollviewoutlet.contentSize = CGSizeMake(375, 840)
.
But I'm not able to get it to scroll at all, it will stay exactly like it opens up with From: Label being the last visible elements.
What am I doing wrong?
You've located the problem, thanks to your logging. (Don't worry about the double call - how many times and when viewDidLayoutSubviews gets called is uninteresting.)
The problem is that you've set the scroll view itself to be taller than the window. In particular, it is 375x809. But so is its contentSize. Thus there is nothing to scroll. A scroll view can only scroll if its contentSize is bigger (in one or two dimensions) than the scroll view. (Moreover, a scroll view bigger than the screen is usually somewhat pointless, since there will be parts of its content that the user will never see - even scrolled all the way, that content will be offscreen.)
So, fix the size of the scroll view itself and you'll be fine. The best way is to assign it constraints that pin its boundaries to its superview, since the superview will have different sizes on different devices.
I designed about 40 view controllers using a 5.5 inch storyboard layout. After all of that I tested it on the iPhone 4S...big mistake. everything is jumbled together being for a larger screen size. I was able to fix one view controller up using Size Classes. I am wondering if there is any way I can adjust all 40 at the same time, or at least avoid doing this for every single one. It is really frustrating finding this out now. Thanks!
This is a relatively complicated issue you are attempting to solve, but I have two potential solutions. Both suggestions are based on moving your current interface into containing UIScrollView instances
If you are using storyboards, then for each of your view controller scenes, put a UIScrollView as a descendent of the view controller's view. From there, provided your subviews are contained within other views (like a container view for a set of buttons), you can move those into your scroll view. You will have to setup constraints to define the size of the scroll view's content, but this will allow the size of the device to have a smaller impact on the interface as you will get scrolling as needed.
If you are using nib files (.xib) then it is essentially the same thing, but easier. In this case, move a UIScrollView onto the canvas, but not as a subview of the default view. Once that is out there, move the original view to be a subview of the scroll view and set constraints to be 0 from the subview to the scroll view. Finally, right click drag from the File's Owner icon to the scroll view and set that as the view outlet.
Hopefully one of these will help you.