I'm in the midst of trying to use a UIScrollView and there appears to be some fundamental thing that I'm just not understanding.
Let's say I want to use a UIScrollView in my iphone app. I have a View filled with buttons that is 320x700. Obviously, this is too big for the iPhone which is 320x480. So I know I have to use a UIScrollView. However, is this the order that I should be creating the objects
Is this right?
This works, but it doesn't make sense to me. I get that the View is supposed to be the canvas, where I add all the UI elements. I want the "canvas" of the iPhone app to be 320x700, and I want to be able to put my buttons, etc on this 320x700 canvas. But if I don't change the size of the UIScrollView back to 320x480, it won't scroll, because I need to set the content size of the UIScrollView larger than its size.
But if I set the size of the UIScrollView to 320x480, then I don't see the screen and the buttons between 480 and 700 in Interface Builder! So it seems like I'm supposed to make all my edits and add all my UI elements to the UIScrollView, and then set it back to the 320x480!
Is there some other way to do this that makes more sense? What am I missing in my understanding of how this should work?
In order to place multiple views in the scroll view, one needs to make a view group(like LinearLayout) as a direct child and then we can define many views inside it. A ScrollView supports Vertical scrolling only, so in order to create a horizontally scrollable view, HorizontalScrollView is used.
Make sure that the subclass must be of type UIViewController. language must be swift. click next and save your cocoa class inside in the main root of your project. Click on the main storyboard and go to the new view controller.
I have posted another solution here which I think is simpler and better.
Here's another way to do this that you might like better:
UIScrollView
as a top-level object in your nib. Set its size to the screen size (320x460) or just turn on a status bar under "Simulated Metrics".delegate
outlet to File's Owner.view
outlet to the scroll view.UIView
as another top-level object in your nib. This will be your content view.contentView
in your view controller (File's Owner) and connect it to the content view.In your view controller's viewDidLoad
, do this:
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.contentView];
((UIScrollView *)self.view).contentSize = self.contentView.frame.size;
}
In your view controller's viewDidUnload
, do this:
- (void)viewDidUnload {
self.contentView = nil;
[super viewDidUnload];
}
Full size
You are right, the View is the canvas where you add all the UI elements. Interface builder is kind of weird at first but you will get used to it, that is just the way it works.
You are getting stuck on the fact that you have to resize the ScrollView. You should think of it like this: The ScrollView has a frame size and a content size. The way it's built is that if the content size is larger than the frame size then it will scroll. You have to make the frame as big as you need to in the interface builder so you can position the elements that go inside. When you run the application you should resize the frame of the scroll to fit inside the iPhone's screen resolution. It doesn't make sense for your controls to have a frame bigger than the screen.
--------------------------------------------
| | | |
| | | |
| | | |
| | | |
| | |<------------------ iPhone Screen frame
| | | |
| | | |
| | | |
| | | |<------- ScrollView Content size
| | | |
| | | |
| | |<----------------- ScrollView Frame Size
| | | |
| | | |
| | | |
| | | |
| | | |
| ------------------------ |
| |
| |
| |
--------------------------------------------
I hope this representation will make it clearer how things should suppose to work.To put it some other way, the scroll frame is the hole trough you can see the content, if the whole is as big as the content then you have no need to scroll cos you can see it all.
I would suggest also trying to write the components in code without using IB to get a better understanding.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With