uiscrollview within a uiview - iphone

I am working on an application where i need to place a uiscrollview within an uiview. The view has a fixed back ground, and in lower half of the view i have to place 30 buttons which scrolls within that area. Lets say the uiview is of 320x480, then the scrollview i have to place will be at (40, 160) to (280, 300). I just need the scroll view to have the 30 buttons inside it. so that view remains fixed, only the buttons can be selected by scrolling vertically. How can i do that?
When i place the scroll view and connect it with file's owner 'view' it eats up all the view space.
Yes, i am a newbie :$

First Add UIScrollView to the view:
UIScrollView *scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(40,160,280,300)];
scroll.showsHorizontalScrollIndicator = YES;
[self.view addSubview:scroll];
// Now Add your Buttons on this scrollview;
[scroll addSubview:buttonObject];]
If you wanted to add 30 buttons, you can use 'for' loops to allocate UIButton's and then add it on scrollview.

The UIViewController sub class (your files owner) will only show the one view you connect to it's view outlet. So you should connect the outer view to files owner, and then add the scroll view to the view by dragging it into the view, and sizing it as you like.
You probably want to create another outlet for the scrollview, so that you also have access to the scrollview from your controller. You could to this by adding the following to you controllers h file and then connecting the scroll view in interface builder.
#property (strong, nonatomic) IBOutlet UIScrollView *scrollView;

Related

iPhone: Make UIScrollView from UIView

I have a UIView with labels and textfields and I tested it in the simulator and noticed that I cannot scroll to see the rest of the view, how can I make the view into a UIScrollView and what do I need to do so I can make the view scrollable? I don't want to move labels and stuff or delete the UIView, is there a simple way?
Take a UIScrollView in Xib file outside your view. drag your view into this scrollview. The subviews inside your view remains same. and set the scrollview's contentSize

UIScrollView with multiple views(which is having button, scrollview and label)

In my app I am using a UIScrollView with multiple views (each view having buttons, scrollview and labels). By using for loop I am loading that views into UIScrollView. But when I tried to get touch actions in views inside UIScrollView only last view is able to access. How can we get touch access to all views inside UIScrollView?.
Any help or suggestion to do this.
Just assign tag value to your all yourView then access your view
UIView *yourView = (UIView*)[ScrollView viewWithTag:tag];
Or
NSArray *yourView =(NSArray*)[ScrollView subviews];
NSLog(#"Your subView %#",[yourView objectAtIndex:index]);

Create UIButton offscreen with Interface Builder

I want to create a big UIScrollView, its contentView should contain more than 30 UIButton. Positions of these UIButton are not rectiligne and cannot be create 'programmaticaly' so I've placed all these UIButton on a UIView manually. I zoom/dezoom and scroll all over my UIScrollView fine BUT here is my problem : UIButton created offscreen is not accessible, I mean I can't click them (only UIButton created in the CGRect(0.f, 0.f, 320.f, 480.f) can be click.
Any suggestion ?
Create a view, put a scrollView inside that view, and place your buttons on the scrollView. While adding buttons and moving them around, be certain that they're always inside the scrollView by looking at the object hierarchy in interface builder.
You can slide the scrollView around as you place things, no need to zoom. As long as your buttons are children of your scrollView it should work fine.

Setting static content in a UIScrollView through XIB

I need a UIScrollview to show some static content in my app. I have taken a UIScrollView through XIB and started adding some UIImageViews and textViews in to it. But after coming to the end of the scrollView the view is not exapanding anymore. I need more space so that I can add some more views below. Is there any way in which I can do this (through XIB and not through code).
I struggled a lot to get this done in a more elegant way then the solution described by Csabi.
It's really simple:
In your xib file just add a generic view which is not a subview of your viewController.view (i.e although this view is in your xib file, it is not a part of your viewController's view hierarchy.)
Then select this view and using size inspector set the width and height that suits your need. Add whatever objects you want to this view. Hook up this view to your viewController with an IBOutlet. (Let's call it IBOutlet UIView *myBigView).
in your viewController.view drag a ScrollView and adjust the size of the scroll view as you like. Hook this up to your viewController. (IBOutlet UIScrollView *scrollView)
Now it's super simple:
-(void) viewDidLoad{
[super viewDidLoad];
self.scrollView.contentSize = self.myBigView.bounds.size;
[self.scrollView addSubview:self.myBigView];
}
Yes it is you can define the height of the UIScrollView simply for example height:1800
now you get bigger UIScrollView then your view you can put your objects to scrollview, and if it is filled simply pull it upward and drag and drop other items when you are finished simply pull the scroll view to its position and you have it.
It is simple :)
Hope it helps
Ask if you have any other question

How to set up UIScrollView in Interface Builder with UI elements outside the main iPhone view?

I am building a data entry form in my iPhone app, and there are more data fields than will fit on the screen. I figured I should put them into a UIScrollView so that the user can scroll through the form. What's the best way to build this in Interface Builder? I know that I can do it programmatically, but I'd like to do it in Interface Builder if I can. The problem is, how can I lay out UILabels, UITextFields, etc. if they fall outside of the main iPhone screen--in the part of the screen for which the UIScrollView becomes useful?
The best workaround I found for this problem (which I consider embarrassing for Interface Builder) is this:
place a "container" UIView inside the UIScrollView, set the size of the container UIView to what is needed (e.g. 320 x 1200) with the inspector and add your content inside that container view. (your buttons, textfields, etc).
set the contentSize for the UIScrollView, in code, to be the same as the size of your container UIView. somewhere in viewDidLoad for example (e.g. scrollView.contentSize = containerView.frame.size;)
To modify content beyond the scrollview's bounds in Interface Builder, you have to drag your container view outside of the scroll view each time, make your modifications, then drag your container view back inside the UIScrollView and build.
This is actually straightforward:
Create a new view controller class e.g. MyScrollViewController
Create a new xib with a UIScrollView as the topmost view, and set the class of the File's Owner to MyScrollView Controller
Set the view attribute of File's Owner to the scroll view
Drag the bottom of the scrollview to create the desired size.
Position your various other UI elements as sub-views of the scroll view
Create and connect an IBOutlet in MyScrollViewController.h for the scroll view, e.g.
#property (retain, nonatomic) IBOutlet UIScrollView *scrollView;
In MyScrollViewController's viewDidLoad method, add the following line of code:
self.scrollView.contentSize = self.scrollView.frame.size;
Th-th-th-that's all folks!
Set up "Content Insets" of "Scroll View Size" on the "Size inspector": Bottom = YourFrameHeight - ScreenHeight. It will allow you to scroll in ranges of top-to-top, bottom-to-bottom of your UIScrollView
Double click to open the UIScrollView itself in IB, and increase the size to the size you need or bigger (you can always shrink at runtime). Then just add the elements.
EDIT:
Doesn't work - see here instead.
I've had the same issue as well. What I ended up doing was putting another UIView inside of it and setting the height to whatever I wanted. Then I put all my UI elements inside of it. This allows you to drag the inner view up and down.
What worked for me in xCode5 (no storyboard, using autolayout) is using the 7 step answer above the ends with 'Th-th-th-that's all folks!' and adding two steps.
8) Drag a new UIView to interface builder. Not into the Scroll view. Just on its own. Put all your controls/view into that and make it as big as you want. I hooked up this view as contentView.
#property (weak, nonatomic) IBOutlet UIView *contentView;
9) Then in - (void)viewDidLoad
[self.mainScrollView addSubview:self.contentView];
[self.mainScrollView setContentSize:CGSizeMake(self.contentView.frame.size.width,self.contentView.frame.size.height)];
Just saying that made it work for me.
There is finally a sensible solution to all of this and that is to place a Container View inside of the UIScrollView. As Interface Builder displays the UIViewController inside the Container View separately, you can finally see what your UIScrollView content is going to look like without crossing your fingers and standing on your head.
You still need to programmatically set your content size but a least you can now visualise what it going on.