Adding views to a scrolling view - iphone

I have a view (myContactViewController.xib) which contains a Scroll View (created in Interface Builder, and which takes takes up the entire height of the window and is 960px in width — ie. 3 x the width of a single window). I'm using the Scroll View, with paging, for horizontal navigation of three subviews (all three subviews are 320x460, ie. the entire size of the window). I have created these three subviews via Xcode (command-N), and so for each subview I have a .h, .m and .xib. I have used Interface Builder to build the layout (just some text and a picture for now — but, will eventually contains some buttons) for their views.
Programmatically, I would like the load these three views into my Scroll View, specifying their location within. The .xibs for each of the views I'd like to load into the scroll view are as follow: myContactSub1.xib, myContactSub2.xib and myContactSub3.xib).
I'm wondering if anyone would be willing to explain how to do this?
Thanks,
Kristin.

Sure it's very easy. First set the frame for the subview. The frame specifies where in the superview you want it to appear and is a CGRect. So you might use CGRectMake(0, 0, 320, 460) for the first subview. Then add it to the UIScrollView with addSubview:. Here's an example:
mySubView.frame = CGRectMake(0, 0, 320, 460);
[myScrollView addSubview:mySubView];
This assumes you have a pointer to these views, using IBOutlets. If you need help doing this read up on Xcode Integration
Also take a look at View Geometry

Related

Cannot change height of UITableView

In the Apple iPhone Clock App, when a user adds a new alarm a modal view pops up. I am trying to create a UI similar to that.
I currently have a UITableViewController as the root view controller of a UINavigationController.
I also have a UIDatePicker added as a subview to the UINavigationController:
[self.navigationController.view addSubview:mydatePicker];
However, I have about 10+ rows in my UITableview (that of the UITableViewController) and as soon as I added the UIDatePicker I cannot scroll to view all the cells.
I realized that the UITableView size is the same size as it was before I added the UIDatePicker, and therefore I would need to change its size in order to be able to scroll to see all the table cells.
I have tried several things in order to change its size, all to no avail. In the code below I arbitrarily chose 50 for the new height.
First, tried changing the bounds height:
CGRect bounds = [self.tableView bounds];
[self.tableView setBounds:CGRectMake(bounds.origin.x,
bounds.origin.y,
bounds.size.width,
50)];
Then tried to change the frame height:
CGRect tvframe = [self.tableView frame];
[self.tableView setFrame:CGRectMake(tvframe.origin.x,
tvframe.origin.y,
tvframe.size.width,
50)];
Then after googling some more i tried changing the contentSize height:
CGSize thesize = self.tableView.contentSize;
thesize.height = 50;
self.tableView.contentSize = thesize;
None of these appeared to have any effect on the size of the UITableView. I still could not scroll to see all the cells.
I later tried some of the same methods as above but on the UINavigationController instead of the UITableView. I didn't have much luck with this either.
As a last resort, I tried to change the size of the UITableView in the Storyboard editor. I could not figure this out either.
Thanks for any help you can provide.
From the UITableViewController class reference, it:
creates an unconfigured UITableView object with the correct dimensions
and autoresize mask
And a quote from the Table View Programming Guide for iOS which specifically addresses this behavior:
Note: You should use a UIViewController subclass rather than a
subclass of UITableViewController to manage a table view if the view
to be managed is composed of multiple subviews, only one of which is a
table view. The default behavior of the UITableViewController class is
to make the table view fill the screen between the navigation bar and
the tab bar (if either are present).
If you don't want the table view controller setting the size of your tableView, then you need to use a UIViewController instead. See the link that I posted above to the Table View Programming Guide for iOS for other things to consider when going this route.
I had same problem as you mentioned.
I was unable to change the height of tableView of UITableViewController. I tried changing frame and centers of self.view and self.tableview. But failed.
Then I solved it by a trick, changing tableview's contentInset.
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 44.0f, 0); // here you can replace 44.0f with your height
You need to make the UITableView a subview of your main view in order to change its size reliably.
The frame is the correct value to alter once you have made this change.
I dont know what is your real Problem . but for change hight of your UITableView and will Display all rows in UITableView write following code.
self.tblView.frame = CGRectMake(0, 0, 320, 50);
please try it.
Your are taking uitableview controller, in place of that use uiviewcontroller in that add the uitabelview. Now you can the the size of uitableview.

scrolling not working for views inside tabbar application

I have created a tabBar application, it has two tabs one tableView with detail items and another simple UIView with a UILabel. I added scrollView to detailViewController and put all UI items under scrollView to get scrolling. I connected the scrollView using outlet to my detail view and set the following in detailViewController
[scrollView setScrollEnabled:YES];
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width,scrollView.frame.size.height);
However detail view is not scrolling, its screen is locked. why?
For a UIScrollView, you want the frame to be the size you want it to appear on the screen, and the content size to be the size it will be able to scroll through. So, for scrolling to work properly, you must have the content size set to larger than the view's size in one dimension or the other.

How to use UIScrollView in IB? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
steps for creating UIScrollView with Interface Builder
I am using UIScrollView for one of my view. I have added view first and then scroll view, then some controls over scroll view, all are added through IB. Every things works fine until the page is fit, some controls need to be added down that is exceeding the UIView length, I am not sure, how to add controls in IB in that case, So I have added programmatically, but my scrollview is not scrolling down, it is scrolling upto my view height, I have tried setting the new view height, scrollview's frame and content size nothing works, Can anyone please suggest me what to do now...
i) how to add control in IB more than its view length.(view height are disabled).
ii) How to make my scrollview which covers full view.
Create the view for the UIScrollView separately.
Technically, UIScrollView can hold controls directly in it. But that is not the point of it. You should always (if using IB) design the view separately and then place it inside the UIScrollView and set the contentSize property to the size of that view.
So, instead of putting contents directly in the UIScrollView in the main view, drag a separate view from the IB objects library on the nib (not on the main view).
Set the width of this view to the width of UIScrollView. Start placing controls on it and extend the height of view. For every new control to be placed, the height must be increased so as to hold that control. You should not place control outside a view from IB. (Although you can).
After designing the view, drag the view inside the UIScrollView and set the Y value of this view to 0. (When you drag a view inside UIScrollView it seems to upset it's Y value).
Now programmatically set the contentSize of UIScrollView to the size of this view. For this you'll need to create an IBOutlet for the UIView you just created.
myscrollview.contentSize = myview.frame.size;
And you're done.
-EDIT-
it is so simple. Follow the steps I'm giving you below you will get what you want.
decide the actual size that is the size in which you want your scroll view to display. say it is 100 * 5o.
set your UIView size also 100 * 50.
Now increase the scrollview hieght in nib.
add what you want
Now check upto what size you have reached.
Suppose it is reached upto 300
So set your contentsize 100 * 300 or 100 * 310 programmatically
In nib set scrollview size 100 * 50
Enjoy programming.
It's very simple, first remember your UIScrollView Frame and then just change UIScrollView frame's Y-position by subtracting 100 (e.g 0 to -100) and add same to height(460 to 560). Then simply add you controls at your required position by continuing this steps...
For eg:
ScrollView Frame (0,0,320,460)
after CHANGE:
(0,-100,320,560)
After finishing with controls adding simply set previous frame like:
(0,0,320,460)

Using Interface Builder to add subview

My questions below are about doing this with Interface Builder, not in code.
I have a XIB with two views. One of them is default view (Table View) and I add the second (Activity Indicator View) as a subview in code. my questions are:
Is it possible to set the location of the second subview using IB, not in code? (i.e. can I remove the code line: activityIndicator.frame = CGRectMake(160, 240, 30, 30);)
Is it possible to add the second subview as a subview using Interface Builder? (i.e.e can I remove the code [self.view addSubview:self.activityIndicator])
Yes, to both questions.
The xib editor has a "Size inspector" that looks like a ruler icon near the top right of the Xcode window. That allows you to specify the size and location of a view inside its parent view. There are also controls that determine what happens to the subview when its parent changes size...on rotation, for example.
You can make a view into a subview either by dragging it into the parent in the layout section of the editor screen or by arranging views in the hierarchy window under "Objects".

Difference between directly setting a controller's view and adding view as a subview

I have just started learning objective-C and the iphone sdk and I have a question that I hope someone can help shed some light on.
What is the difference in the following:
self.view = someView;
and
[self.view addSubView: someView];
Say for example, in a simple app, where we have only one controller and one container view (has a few image subviews).
What is the difference between the two statements? The reason that I'm asking is because I was tinkering around in some sample code and I noticed the view was being initialized with images as subviews like so:
if (self = [super initWithFrame:CGRectZero])
{
//adds some images as subviews here
}
As I understand it the initWithFrame: CGRectZero, creates a frame with size at [0,0,0,0] (essentially invisible).
When I directly set the view with
self.view = someView;
I notice the view actually displays the image. But when I add the view to as a subview of controller's 'default' view, it doesn't. So basically my question is, whats going on behind the scenes? Why is the first method "resizing" the frame and the second one not doing the same thing.
What you see on the screen of your iPhone is almost always a hierarchy of views.
When you look at, say, your inbox in Mail, you're seeing a bunch of views. There's a big containing view.[1] Within that, there's a navigation bar view, a table view, and a toolbar view. Within the navigation bar view, there's a button view on each side and a label view in the middle. Inside the table view, there are a bunch of table cell views, and each of those cells has several label views. The toolbar has five button views. I could go further and talk about the views inside those buttons and so on, but I'm sure you get the idea.
The view above any given view is its superview; the views below it are its subviews. So a table cell view has a table view as its superview and a bunch of label views as its subviews. The top view, the one that has all the other views inside it, is called the root view.
Each view has its own drawing surface. The rectangle formed by that drawing surface is called the frame. The frame of a view is relative to the frame of its containing view. So if one of our table cell's label subviews has its frame at (0,0), that means it will be in the table cell's top left corner, even if the cell is halfway down the screen.
When you're writing a view controller, self.view is that root view I mentioned earlier; all the other views are subviews of that one (or subviews of its subviews, etc.). One of the features of a view controller is that it automatically resizes its self.view to fit the screen. (The available area will be smaller in the middle of a phone call: the status bar is twice as high then, so there's less space for your app. It will also be smaller if your view controller is being managed by a navigation controller or tab bar controller, but that's a different story.) But just because you resize its root view doesn't mean that the root view's subviews will automatically resize. To do that, you need to set their autoresizing mask (a property which tells the view how it should react when its superview changes size):
someView.autoresizingMask = UIViewAutoresizingFlexibleWidth
| UIViewAutoresizingFlexibleHeight;
(There's a graphical way to set up the autoresizing mask in Interface Builder—click the ruler icon in the inspector window and look at the "Autosizing" section.)
Even that's not enough, though, if someView isn't the right size to start with. To do that, adjust its frame before you add it as a subview of self.view:
someView.frame = CGRectMake(
0, // all the way to the left
0, // all the way at the top
self.view.frame.size.width, // same width as the root view
self.view.frame.size.height, // same height too
);
So why would you ever use subviews if you have to do all this twiddling that the root view does for you? Simple: you can only have one root view, but one view is almost never enough for what you need to do. If you really need only one view, of course, you can just set it as the root view and go on your merry way, but chances are, things are more complicated than that.
[1] I'm simplifying a bit here, but that's fine for right now.
When you add a view as a subview, you need to make sure that you're actually adding to an existing view.
self.view = view sets the controller's view. Without this (either in code or done with a XIB) you'll never see anything as the controller has no view to show.
[self.view addSubView: someView] assumes that self.view is already set. If it doesn't, you're adding someview as a subview of nil, and it will never get seen.
Basically, think of self.view as the big container, and all the subviews are just pieces inside of it. If you don't need any subviews, setting self.view to a UIImageView or UIWebView is fine. If you do need subviews, you'll need a big, empty container view in which to put them.
In your case, I'm betting self.view is never set, and you're adding your image views to nil.
Setting the view controller "view" property only changes the view it is managing.
Adding a view as a subview of another view, actually adds the subview underneath the other view.
They are very different things, as one adjusts a view controller and the other alters a view hierarchy.
As a guess, the reason you didn't see anything the first way was the frame for the subview you were adding was CGRectZero (0 in size).