Why does my UIToolBar scroll with my UITableView? - iphone

I'm having a bit of difficulty trying to build my view. Everything works out great up to the point where I need to insert a UIToolBar into my view. The tableView is placed where I expected it to be placed. The UIToolBar on the other hand, scrolls up and down with the table, it doesn't remain fixed as it should. It also looks rather odd when put on the screen -- I'm guessing because the calculation to place it isn't right? Attached to this question is a screenshot as well as the code I've used to build this. Thanks for your help in spotting out what I'm doing incorrect. Screenshot: http://dl-web.dropbox.com/u/57676/screenshots/broketoolbar.png
The code:
- (void)loadView
{
[super loadView];
// TableViews that wish to utilize tableView footers/headers should override this method.
UITableView *aTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain];
aTableView.autoresizingMask = (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight);
aTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
aTableView.delegate = self;
aTableView.dataSource = dataSource;
self.tableView = aTableView;
self.view = tableView;
[aTableView release];
UIToolbar *toolbar = [UIToolbar new];
[toolbar setBarStyle:UIBarStyleBlackOpaque];
[toolbar sizeToFit];
CGFloat toolbarHeight = [toolbar frame].size.height;
CGRect mainViewBounds = self.view.bounds;
[toolbar setFrame:CGRectMake(CGRectGetMinX(mainViewBounds),
CGRectGetMinY(mainViewBounds) + CGRectGetHeight(mainViewBounds) - (toolbarHeight * 2.0),
CGRectGetWidth(mainViewBounds),
toolbarHeight)];
[self.view insertSubview:toolbar aboveSubview:self.tableView];
[toolbar release];
}

because self.view is tableView onto which you added toolbar.

Related

The right way to implement loadView?

I have a question regarding the implementation of loadView:
Right now, I have it like this:
- (void)loadView
{
image = [UIImage imageNamed:#"plan.gif"];
scrollView=[[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
imageView = [[UIImageView alloc] initWithFrame:
CGRectMake(0, 0,scrollView.frame.size.width + 40, scrollView.frame.size.height)];
imageView.image = image;
[imageView setContentMode:UIViewContentModeCenter];
scrollView.contentSize = image.size;
scrollView.maximumZoomScale = 3.0;
scrollView.bounces = NO;
scrollView.delegate = self;
// do any further configuration to the scroll view
// add a view, or views, as a subview of the scroll view.
[scrollView addSubview:imageView];
// release scrollView as self.view retains it
self.view=scrollView;
[scrollView release];
}
I suppose some of it should be in viewDidLoad:?
Thanks in advance.
This seems fine to me.
viewDidLoad is normally used as your hook after getting a view returned from IB. In this case you are essentially just doing the work of IB in code (setting up the view heirachy and configuring it).
Therefore in this case I think splitting out the logic may be superfluous unless it makes your code more readable.

Why am i getting a black frame and not the picker?

I have this very simple code or at least i think it's simple.
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
CGRect rect = CGRectMake(0.0f, 20.0f, 320.0f, 216.0f);
UIPickerView *myPickerView = [[UIPickerView alloc] initWithFrame:rect];
self.view = myPickerView;
[myPickerView release];
}
I am running a general View Based template with XCode. I can see the loadView getting called but i get an black frame instead of the UIPickerView.
What am i doing wrong ?
/donnib
Have you implemented the picker's datasource methods? You need it otherwise it won't show.
Link to Apple Documentation here: http://developer.apple.com/library/ios/#DOCUMENTATION/iPhone/Reference/UIPickerViewDataSource_Protocol/Reference/Reference.html#//apple_ref/occ/intf/UIPickerViewDataSource
You've forgotten to set the UIPickerView's delegate to your current view. Add:
myPickerView.delegate = self;
...following your initialization and before your view. And of course, make sure you set up your header file as a UIPickerView delegate and implement the dataSource methods.
Try this:
UIView *contentView = [[UIView alloc] initWithFrame: [[UIScreen mainscreen] bounds]];
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame: CGRectZero];
CGSize pickerSize = [pickerView sizeThatFits: CGSizeZero];
pickerView.frame = [self pickerFrameWithSize: pickerSize];
[pickerView setShowsSelectionIndicator: YES];
[contentView addSubview: pickerView];
[pickerView release];
[self setView: contentView];
[contentView release];
Instead of overriding -loadView, you should override -viewDidLoad. Once the view has loaded you'll create the picker and add it as a subview of the view owned by the view controller.
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect rect = CGRectMake(0.0f, 20.0f, 320.0f, 216.0f);
UIPickerView *myPickerView = [[UIPickerView alloc] initWithFrame:rect];
[self.view addSubview:myPickerView];
[myPickerView release];
}

How to programmatically create UIView -> UIViewController -> UITableView

I've been struggling to add a UIView above my UITableViewController. Through searches, reading and experimenting I've determined that instead of a UITableViewController I should just be using a UIViewController. I'm having a hard time making this work for a variety of reasons and I'd like to just start fresh with a better architecture.
Basically I'm looking for sample code / tutorials that could help me create the following completely programmatically (no NIBS):
- Navigation-based Layout
- UIViewController
-- UIView
--- UITableView
--- Etc.
The reason why I want a UIView above my UITableView is I want to be able to add UIViews above my table.
-UPDATE-
Adding code to make this more clear:
JMoviesListViewController.m - UITableViewController subclass
- (void)loadView
{
NSLog(#"loadView called");
UIView *baseView = [[[UIView alloc] init] autorelease];
TISwipeableTableView * aTableView = [[[TISwipeableTableView alloc] init] autorelease];
[aTableView setDelegate:self];
[aTableView setDataSource:self];
[aTableView setSwipeDelegate:self];
[aTableView setRowHeight:54];
[baseView addSubview:aTableView];
self.view = baseView;
[super loadView];
}
- (void)viewDidLoad {
listOfMovies = [[NSMutableArray alloc] init];
UIView *myProgView = (UIView *)self.progView; // progView is a method that returns a UIView
[self.view insertSubview:myProgView aboveSubview:self.tableView];
[self.navigationItem setTitle:#"Now Playing"];
movies = [[Movies alloc] init];
movies.delegate = self;
[movies getMovies:[NSURL URLWithString:apiQuery]];
[super viewDidLoad];
}
- (UIView *)progView {
if (progView == nil)
{
// create a progress view
//x,y,w,h
progView = [[UIView alloc] initWithFrame:CGRectMake(110, 110, 95, 30)];
progView.backgroundColor = [UIColor grayColor];
progView.tag = 1; // tag this view for later so we can remove it from recycled table cells
progView.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin);
progView.layer.cornerRadius = 5;
UILabel *activityLabel = [[UILabel alloc] init];
activityLabel.text = NSLocalizedString(#"Loading...", #"string1");
activityLabel.backgroundColor = [UIColor grayColor];
activityLabel.textColor = [UIColor whiteColor];
activityLabel.font = [UIFont systemFontOfSize:14];
[progView addSubview:activityLabel];
activityLabel.frame = CGRectMake(5, 2, 70, 25);
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[activityIndicator startAnimating];
[progView addSubview:activityIndicator];
activityIndicator.frame = CGRectMake(70, 5, 20, 20);
}
return progView;
}
To be clear, the code works fine, the problem is that the cell lines of the table are "bleeding through" the UIView spinner that is inserted with this line:
[self.view insertSubview:myProgView aboveSubview:self.tableView];
leading me to believe that myProgView is not aboveSubview:self.tableView.
Views and controllers are separate things. You can have a hierarchy of view controllers and a hierarchy of views. But they're not interleaved, as the title of posts suggests (I know, Interface Builder displays them as a single hierarchy, but views and controllers are more like two parallel hierarchies).
Anyway, you can easily have a view controller set up whatever views you want in code. Override the loadView method, create a view that you assign to self.view, then add subviews to that view.
For example:
- (void)loadView
{
UIView *view = [[[UIView alloc] init] autorelease];
UITableView *tableView = [[[UITableView alloc] init] autorelease];
tableView.dataSource = self;
tableView.delegate = self;
[view addSubview:tableView];
self.view = view;
}
Your view controller should either inherit UITableViewController or implement the UITableViewDataSource and UITableViewDelegate protocols.
You have to actually specify the layout of your different views, not just add them as subviews. Try reading a tutorial about using AutoLayout programmatically.
You also want to set up all your views in loadView. There you can set the bottom of your extra view to the top of your table view.
- (void)viewDidLoad {
[super viewDidLoad];
UIView *view = [[[UIView alloc] initwithFrame:CGRectMake(set your frame)] autorelease];
UITableView *tableView = [[[UITableView alloc] initwithFrame:CGRectMake(set your frame)] autorelease];
tableView.dataSource = self;
tableView.delegate = self;
[view addSubview:tableView];
}
if you are using **ARC** remove autorelease.

How does autosize work in iPhone SDK?

I'm trying to do the following:
alt text http://img101.imageshack.us/img101/7396/33959221.png
I need to do a lot of custom things, so I subclassed everything.
alt text http://img248.imageshack.us/img248/4201/49422483.png
I'm having issues with autoresize and positioning. For instance the UIToolBar is not at the correct position.
So here is the question:
If you don't use UITableViewController or any other Controller, that takes care of all the positioning and sizing, then you have to do it yourself.
Can someone someone tell me how this works? What do I need to understand?
---- UPDATE:
My custom goals are actually not that important as my question is actually about something else. But OK. So all "elements" like the TableView, the Toolbar and the TabBar will have custom backgrounds and use custom buttons. The TableView will be GroupedStyle and the groups can be collapsed.
My question is how do I have to structure my code especially regarding positioning and autoresizing in order to achieve what you can see in picture #1.
This is how it's structured right now:
Application Delegate:
- (void) loadView {
RootViewController *rootView = [[RootViewController alloc] init];
[window addSubview:rootView.view];
[window makeKeyAndVisible];
RootViewController:
- (void) loadView {
self.theView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.theView.autoresizesSubviews = YES;
self.theView.autoresizingMask = (UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
self.view = self.theView;
ChildViewController *childView = [[ChildViewController alloc] init];
[self.view addSubview:childView.view];
ChildViewController:
- (void) loadView {
self.theView = [[UIView alloc] init];
self.view = self.theView;
}
- (void) viewDidLoad {
self.view.frame = [[UIScreen mainScreen] bounds];
CGRect toolbarBounds = self.theToolbar.bounds;
CGRect viewBounds = self.view.bounds;
CGRect tableViewBounds = CGRectMake(0, CGRectGetHeight(toolbarBounds), CGRectGetWidth(viewBounds), CGRectGetHeight(viewBounds) - CGRectGetHeight(toolbarBounds));
self.view.autoresizingMask = (UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
self.theTableView = [[MyTableView alloc] initWithFrame:tableViewBounds
style:UITableViewStyleGrouped
parent:self];
[self.view addSubview:self.theToolbar];
[self.view addSubview:self.theTableView];
}
MyToolBar:
- (id)initWithParent:(id)theParent {
if ((self = [self init])) {
self.parent = theParent;
CGRect rectArea = CGRectMake(0, 0, CGRectGetWidth([[[self parent] view] bounds]), 44);
[self setFrame:rectArea];
self.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self loadButtons];
}
return self;
}
So this is how the positioning is structured at the moment (messy). It actually works, BUT I have a ModalView which is handled by the ChildViewController. When I have the iPad in Portrait view and I pop up the modal view and dismiss it, then everything works. But when I do the same on Landscape view it rotates the hole view... the ChildViewController by 90 degrees when dismissing the modal view. Very strange. When I load directly ChildViewController in my AppDelegate, then again everything works fine.
You're missing the UIViewAutoresizingFlexibleBottomMargin option in the autoresize mask of your toolbar, for instance.

Why is my UITableView not occupying the bounds of the screen?

I'm building a UITableView programmatically without NIB files here. I'm doing something silly however, as my navigationBar shows up fine, as does my UITableView. However, the UITableView is not properly fitting onto the screen. You'll see roughly 20 pixels separating the UINavigationBar and the UITableView. I set my window's backgroundColor to black, as you can see in this screen shot:
Here's the code to reproduce the problem:
- (void)loadView
{
[super loadView];
// TableViews that wish to utilize tableView footers/headers should override this method.
UITableView *aTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain];
aTableView.autoresizingMask = (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight);
aTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
aTableView.delegate = self;
aTableView.dataSource = dataSource;
self.tableView = aTableView;
[self.view addSubview:self.tableView];
[aTableView release];
// style navigation bar.
//self.navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque;
}
use bounds instead of applicationFrame