How can I set position of 3 UITable and 2 Navigation Bar? - iphone

My XIB has 3 UITable and 3 Navigation bar.UITables are dynamic scale and I use navigation bar to seperate UITable and show title of UITable name.
I want to set first navigation bar under the first table and second navigation bar under second table.
I try to find frame of first table to get position x,y and height of table but it don't work.
- (void)viewDidLoad
{
blah blah...
CGRect table1frame = table1.frame; //But it don't work
}
I don't know where to get each table frame.
please help me or guide me.Thank you very much

I'm not sure I understand what you're trying to do, but keep in mind that the geometry of the view controller's view (its bounds) is not yet set when viewDidLoad is called. The bounds (and frames) of any subviews have not been set either. That means that all frames are pretty much set to zero at this point. And, that may be what you are noticing if you are trying to access the view's frame, or the frame of any of its subviews,
So, if you need to do anything based on the view's geometry (on its frame or the frame of any of its subviews), it is generally done in viewWillAppear.
Your problem may not be where to get the table view's frame, but when to get it. You are looking for it in the right place (the table view's frame property), but you are too early. I hope this is helpful.

Related

Hiding or Removing UIImageView and arrangement (IOS 5)

I have a UIViewController that I designed in Interface Builder. near the top of the View it has a UIImageView and then some other Views (Text etc) below. These are set to anchor to the top of the view.
In code when the view loads I remove the UIImageView in certain circumstances but it still seems to take space or the view's below don't spring up to the top based on their Anchor.
How can I make the Views below move up if the View above is removed?
You can change the frame of the other elements in the view.
in the viewDidLoad method you can use something like that:
-(void)viewDidLoad{
if(imageVisible){
otherElements.frame = CGRectMake(....);
}else{
otherElements.frame = CGRectMake(....);
}
}
I don't know what is the structure of the view, but you might consider nesting all of the other elements in one view so you could change all the elements positions in one command.
Unlike the Android view hierarchies, iOS view hierarchies are all developed in absolute coordinates. When you say a subview is anchored to the top, that means that if the size of the parent view changes, your view will stay in the same relative position to the top of the screen.
In order to make your views below the image view move up when the image is hidden or removed, you will have to manually arrange them yourself to account for the offset

what will happen when addSubView a big view into a small View

There is a xib with a a dark view (called calendarView) in it
Next is I am adding a subView called calendar ( this is a calendar view from this page )
[self.calendarView addSubview:calendar];
And the result is :the calendar is added but it is cover the calendarView
My question :
what will happen when you try to add a bigger view into a small view.
How to make a bigger view fit in the the small view.
in general the subview will draw itself bigger then the superview. if you dont want that to happen you should set the subview's frame to smaller or equal to the superview's bounds.
if the subview is a costume class you made, you should probably change it to be in the right size, otherwise I think the best way to do it is with transform
Nothing exceptional. The bigger view will be placed accordingly to it's frame.
Simple, just by equaling their sizes (and the added view's origin to be (0,0) ).

4 directional scrollview with paging

I need to implement a scroll view which (on a button press) will page either up, left, down or right depending on which button was pressed. Also the user can indefinately page in the same direction which will load the views in a kind of carousel. So I have 3 viewControllers.... viewController 1 is shown first....The user presses left, it shows viewController2, left again shows viewController3, left again is back to viewController 1 etc. and the same for up,down,right.
Does anyone know a good way to implement this? Im open to all suggestions.
Many thanks
Jules
Edit - second attempt at a clear explanation:
Consider this matrix.
This 3x4 matrix is the content area of your scroll view. With paging enabled, your scroll view will stop on one of these "cells", e.g. 2,1. That portion of the scroll view will be visible.
If you want each "cell" to be controlled by it's own view controller, then pre-generate all the view controllers (and their views), and then add all of their views as subviews to the scrollView.
You would populate this scroll view with whatever view you wanted to show at any given location. Set the frame of each view relative to the scrollview's origin. So if the cells were 320 pixels wide and 480 pixels tall, the frame for cell 1,3 would be CGRectMake(1*320, 3*480, 320,480).
When the scrollView ends deceleration, you can get it's contentOffset property, do some arithmetic and figure out which cell you're in.
To get the wrap-around effect, you'll have to do some trickery. You might put an extra cell at the end of each row and column, and if you find yourself in that cell, just set the scrollviews' contentOffset to the corresponding cell at the beginning of the row or column.

What's the point of having to set the frame of a view of an UIViewController when it adjusts it anyways?

From the docs:
Create a root view object that is
sized to fit the screen. The root view
acts as the container for all other
views associated with your view
controller. You typically define the
frame for this view to match the size
of the application window, which
itself should fill the screen.
However, the view controller also
adjusts the frame size as needed to
accommodate the presence of assorted
views, such as the system status bar,
a navigation bar, or a tab bar.
Why should I bother then to set the view's frame to the application window size? I mean... if the view controller adjusts it anyways, then why should I? No...wait... I get it... it makes sense to set it, because subviews created right after that in -loadView might want to know the frame. But doesn't make a lot of sense since that should be done in -viewDidLoad, right?
The designated initializers for UIView both require a frame be provided either from nib or programatically. This is most likely because all subsequent drawing of any kind depends on the frame. The frame also defines where in the window/super-view the view will appear and even if it should be drawn at all.
In other words, having no frame attribute will break a lot of the class' default methods. even if the frames gets changed later, at any point in time the instance must have a frame attribute.

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).