I have a UIViewController with a UITableView on top of it. I am trying to put a small UIView overlay on top of the table view. I added it both programatically (and used bringSubviewToFront), and then in Interface Builder and it does not appear either way.
Why is that? How can I add the UIView overlay on top of the table view?
EDIT:
I am not using a UITableViewController. It's a UIViewController with a UITableView. Here is the code I used in viewDidLoad:
UIView *joinMediame = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 320, 44)];
joinMediame.backgroundColor = [UIColor orangeColor];
[self.view addSubview:joinMediame];
[self.view bringSubviewToFront:joinMediame];
If you are intending for your view to scroll with the table content, I would make it the tableHeaderView of the table. If you want the view to be static at the top, I would add it as a subview of the controller and then resize the table view frame to make room for the static view.
Note that if the controller is a UITableViewController, self.view is a UITableView, so if you do something like [self.view addSubview:staticView] this won't have the effect you expect.
Related
hi i'm studying iOS programming.
i have a wondering using interface builder.
i make a tableViewController and also make .xib file
now i can see the UITableView in interface builder.
i have to add a view, called myView, that contains buttons, labels and so on.
i want to myView be set the top of tableView's area, like tableview's header.
so i make a myView, and add buttons, labels, etc.
and i drag that view into UITableView. ok here's fine.
i can see myView is set the top of UITableView in interface builder.
but i run the program, myView doesn't appear.
of course wire up using IBOutlet, and declare property and synthesize.
but i use NSLog like this
if(self.myView == nil)
NSLog(#"omg it's nil!!!");
i can't understand that NSLog is printed my prompt area.
because i make that view in interface builder!
i can see tableView, of course can see the cells.
why myView doesn't appear??
how can i fix it??
i want to know how can i fix it using interface builder.
please help me
Not sure if this is possible using interface builder, I usually create a view manually and add it to the tableview header in the viewWillAppear method like so:
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.bounds.size.width, 60)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 10,self.tableView.bounds.size.width, 50)];
label.text = [person getFullName];
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:25];
label.shadowColor = [UIColor whiteColor];
label.shadowOffset = CGSizeMake(0,1);
[headerView addSubview:label];
self.tableView.tableHeaderView = headerView;
A simpler method would be to create a view in a separate nib file and then load it into the table header when you load the tableview like this:
UIViewController *aViewController = [[UIViewController alloc] initWithNibName:#"MYXIBFILEHERE" bundle:nil];
self.tableView.tableHeaderView = aViewController.view;
My experience with XCode 4.3:
you have to first place UITableView and UIView on the same super UIView. Then, when dragging UIView to the top of UITableView you'll notice XCode highlighting the option of inserting your view as footer.
Once done, you can detach UITableView from superview and delete it.
The only way I've been able to get this to work is as follows:
Add new nib file to project. Lay out the view in the nib file as you
want your table footer/header to look.
Set the class of the File's Owner in the nib to the class of your
UITableViewController. This is so we can connect the view in this
nib to an IBOutlet in the UITableViewController class.
Set a UIView IBOutlet in your UITableViewController to point to the
view in the new nib.
In your UITableViewController class, do something like this in
viewDidLoad:
(apparently the code won't format correctly if it's directly after a numbered list.)
[[NSBundle mainBundle] loadNibNamed:#"statsFooterView" owner:self options:nil];
self.tableView.tableFooterView = myFooterView;
I build an UIViewController, that higher than iPhone screen ( the height is 900px ), i build it using UIScrollView.
In the Interface Builder, how to put an View in the bottom part of the UIScrollview ? i've tried using freeform in the inspector -> Simulated Metrics, but every time i move back to the ipad/iphone screensize, the views are mess. And when i ran it in the simulator, the view still a mess
I'd add a view programmatically and make it subview for the scrollview, i don't know how to do it using IB.
If you want to try programmatically can try something like:
UIView*myView=[[UIView alloc ] initWithFrame:CGRectMake(0, 480, 320, 200)];
[myView setBackgroundColor:[UIColor redColor]]; // Just for testing purpose
[scrollView addSubview:myView];
You have to do a bit of it programatically I'm afraid but you can do most of it in a xib.
Your xib would contain a UIView that's your normal view, containing your scrollview.
It would also contain another UIView; this will be as long as you want and will be your scrollview's contents.
In your .h file, have a property like this and attach it to the second UIView in your xib
#property (nonatomic, retain) IBOutlet UIView *scrollingContents;
and in your viewDidLoad, that's where you attach the contents inside the scroll view
- (void)viewDidLoad {
[super viewDidLoad];
[self.scrollView addSubview:scrollingContents];
self.scrollView.contentSize = scrollingContents.bounds.size;
}
When I add a subview to my UITableViewController, it seems to be underneath the tableview. I may be loading my subview incorrectly, or calling addSubview in the wrong place. The subview I'm referring to is the red area above the tabbar that also contains the "Click me" button:
You can see that the cell lines kind of overlap. Here is where I'm adding the subview in my TableViewController:
- (void)viewDidLoad {
[super viewDidLoad];
CGRect hRect = CGRectMake(0, 170, 320, 480);
HomeScreenOverlayView *homeView = [[HomeScreenOverlayView alloc] initWithFrame:hRect];
[self.tableView addSubview:homeView];
[homeView release];
}
Any ideas? Thanks!
I have had this issue myself and resolved it by not adding a view to the table, but rather adding the view to the table's superview.
UIView *viewToAdd = [[UIView alloc] initWithFrame: self.view.frame];
[self.view.superview addSubview: viewToAdd];
This is particularly useful when you want to mask the entire table (e.g. loading screens).
N.B. I will usually add this to viewWillAppear: in the view's lifecycle.
When you call addSubview, it is probably the first view added to the table view. Later, all the cells and support views will be added over your view.
The best thing to do is create an empty view and add both the table view and overlay view to it, making sure the overlay view is above the table view.
Views serve the 3 roles of drawing, interaction and layout. It is fine to have a view that only fills one of those roles.
You can use - (void)sendSubviewToBack:(UIView *)view or - (void)insertSubview:(UIView *)view atIndex:(NSInteger)index. addSubview always puts the new view in front.
EDIT: Sorry, misread the question, it looks like you want the subview to be in front.
I have a UITableView which is not being resized properly using autoresizeMask (in iPhone 3.0).
The UITableView is inside a UIViewController inside a UINavigationController inside a UITabBarController, all of which are being created programatically. The status bar is visible.
The code of the UIViewController is basically:
- (void)loadView {
UIView* rootView = [[UIView alloc] init];
self.view = rootView;
[rootView release];
}
- (void)viewDidLoad {
[super viewDidLoad];
// table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480-20-49-44)];
table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)]; table.autoresizingMask = UIViewAutoresizingFlexibleHeight;
[self.view addSubview:table];
}
When created like this, the UITableView is slightly bigger than the available space. If I'm not mistaken, it's exactly 44 pixels bigger, the size of the navigation bar.
However, if I uncomment the commented line and comment the next line the size of the UITableView is exactly right. I would prefer to use autoresizingMask instead of manually calculating the size of the UITableView. What am I doing wrong?
Thank you in advance!
The problem seems to be that I wasn't setting the frame of the root view in loadView. If you define such frame, and then define the frame of the subviews in relation to that frame, then the autoresize masks will correctly resize the subviews according to how the root view was resized by the framework.
For example:
- (void)loadView {
UIView* rootView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
self.view = rootView;
[rootView release];
}
- (void)viewDidLoad {
[super viewDidLoad];
table = [[UITableView alloc] initWithFrame:self.view.frame];
table.autoresizingMask = UIViewAutoresizingFlexibleHeight;
[self.view addSubview:table];
}
Thanks to Colin Gislason who pointed me in the right direction.
The autoresizing mask will not help you with the initial size of the table view. The table view is created with the frame that you give it. The autoresizing mask defines the rules for resizing this frame relative to the parent view when the parent's frame changes.
So if I define a table that is 320x100 it will stay that size unless I change it explicitly or the parent view's frame changes.
Depending on the other views, you could do the calculation based on the other views held by the parent or by the parent's frame itself.
Create UIViewController subclass instand of UITableViewController Subclass.
insert UITableView instance.
in NIB simply drag and drop UIView
on top of that wier place the existing UITableVIew object.
set the size of the uitableview via nib or viewDidLoad method.
set the reference , dataSource and delegate via nib.
now its simply transfer the UIViewController class and the can change tableview size as you wish.
First a little background info:
I have UIViewController that contains a UITableView. In the loadView method (after initialization of the table), I set the UIViewControllers view to the table view with: self.view = tableView;
What I want is a view on the top of the screen (before the UITableView), that doesn't scroll with the rest of the table view when it is scrolled. I have tried adding my UIView to the table view's tableViewHeader, which displays correctly but scrolls with the rest of the table.
Is there any easy fix for this? Either way, any hints towards a solution is greatly appreciated.
EDIT:
Come to think of it, what I want is something like the stock application where the bottom part is stationary and the rest of the screen is a UITableView. The only difference is that I want the stationary part at the top of the screen.
As kmit has already pointed out, you can easily add more than one subview to your view. So, don't set the table view directly as self.view, but rather create a blank UIView (as container) and add the table view as well as the header view as subviews to that view. You can control the views' extents via their frame attributes. A simple example:
- (void)loadView {
UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
[view setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth];
// header view
HeaderView* headerView = [[HeaderView alloc] initWithFrame:CGRectMake(0, 0, 320, 182)];
self.headerView = headerView; // in case you need the reference later on
[view addSubview:headerView];
[headerView release];
// table view
UITableView* tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 182, 320, 186) style:UITableViewStylePlain];
[tableView setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth];
tableView.delegate = self;
tableView.dataSource = self;
[view addSubview:tableView];
self.tableView = tableView;
[tableView release];
self.view = view;
[view release];
}
As an alternative to creating the containing UIView manually, you can call [super loadView] at the beginning of your loadView implementation.
Is there a reason you are setting the view of the UIViewController to that of the UITableView? Why not handle the UITableView as a subview? That would allow you to add anything you want above the UITableView -another view, empty space with the view of the UIViewController as your background, etc.