Subview appears behind tableview - iphone

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.

Related

Resize table view

I'm searching for a way to have a UITableViewController with a UITableView at the top and a UIPickerView bellow (with fix position).
I've found a solution for fixing the picker with the code bellow:
- (void)viewDidLoad {
[super viewDidLoad];
_picker = [[UIPickerView alloc] initWithFrame:CGRectZero];
_picker.showsSelectionIndicator = YES;
_picker.dataSource = self;
_picker.delegate = self;
// Add the picker to the superview so that it will be fixed
[self.navigationController.view addSubview:_picker];
CGRect pickerFrame = _picker.frame;
pickerFrame.origin.y = self.tableView.frame.size.height - 29 - pickerFrame.size.height;
_picker.frame = pickerFrame;
CGRect tableViewFrame = self.tableView.frame;
tableViewFrame.size.height = 215;
self.tableView.frame = tableViewFrame;
[_picker release];
}
The problem is with the tableview, it seems resizing doesn't work so I can't see all results .
Thanks for your advice.
You should use a UIViewController subclass instead of UITableViewController to manage a table view if the view to be managed is composed of multiple subviews, one of which is a table view. You can add a UITableView subview and make your controller implement UITableViewDelegate and UITableViewDataSource protocols.
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).
From Table View Programming Guide for iOS:
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, 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 decide to
use a UIViewController subclass rather
than a subclass of
UITableViewController to manage a
table view, you should perform a
couple of the tasks mentioned above to
conform to the human-interface
guidelines. To clear any selection in
the table view before it’s displayed,
implement the viewWillAppear: method
to clear the selected row (if any) by
calling deselectRowAtIndexPath:animated:.
After the table view has been
displayed, you should flash the scroll
view’s scroll indicators by sending a
flashScrollIndicators message to the
table view; you can do this in an
override of the viewDidAppear: method
of UIViewController.

How to add 2nd UITableView programmatically from within a UINavigationController

I realize this is a popular topic, and I've searched through many posts here but haven't found anything that has helped my issue. I'm a beginner, for what it's worth (as you'll see by my question :-)
My app has a tab bar with 3 items. The first loads a UINavigationController that is intended to have 3 "screens" to drill-down through (first: UITableView, second: filtered UITableView, third: UIView). I cannot, for the life of me, figure out how to show the UITableView on the 2nd screen, programmatically.
I'm overriding - (void)loadView since I'm not using IB. At different times, I've tried things like:
- (void)loadView
{
[super loadView];
// first option (thought for sure this would work)
[[self view] addSubview:secondTableView];
// another...
[self tableView:secondTableView];
// another...
[[[[self navigationController] topLevelController] view] addSubview:secondTableController];
}
I do have the table view setup properly with it's delegate and datasource, I just can't figure out how to show the damn thing. The 2nd controller is also inheriting from UITableViewController. Additionally, I don't know how you can say "fit this table view within the navigation title and the tab bar menu". I'm using CGRectMake() currently to guess the sizes, but it seems like there should be a better way (maybe that's why you use IB :-). Either way, that's secondary to even getting something to show up in the first place.
Thanks in advance for any suggestions.
If you're inheriting from UITableViewController your view should be a UITableView not a plain UIView, then when you want to create a view programmatically you should allocate it, initialize it, set its properties and assign it as your controller's view, I haven't tried [super loadView] before but it's creating the tableView for you and assigning dataSource and delegate to self, so you don't have to do that. So it should go like this
- (void)loadView {
UITableView *tempPointer = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480) andStyle:UITableViewStylePlain];
self.view = tempPointer; // controller retains view property so we should release it
[tempPointer release];
// If you want your view to be resized automatically (to fit)
self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
self.tableView.dataSource = self;
self.tableView.delegate = self;
//...
}
or
- (void)loadView {
[super loadView];
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
secondTableView = self.tableView; // or change your code to cope with self.tableView instead of secondTableView...
//...
}
Clear out some uncertainties about view hierarchy creation, retaining etc in the documentation about both UIViewController and UITableViewController if you haven't already.
have you tried:
[Self.navigationController pushViewController:secondTableView];
Hope this helps, if not could you give a little more describing the situation.

UITextField subview of UITableViewCell to become first responder?

I have a core data application which uses a navigation controller to drill down to a detail view and then if you edit one of the rows of data in the detail view you get taken to an Edit View for the that single line, like in Apples CoreDataBooks example (except CoreDataBooks only uses a UITextField on its own, not one which is a subview of UITableViewCell like mine)!
The edit view is a UITableviewController which creates its table with a single section single row and a UITextfield in the cell, programatically.
What I want to happen is when you select a row to edit and the edit view is pushed onto the nav stack and the edit view is animated moving across the screen, I want the textfield to be selected as firstResponder so that the keyboard is already showing as the view moves across the screen to take position. Like in the Contacts app or in the CoreDataBooks App.
I currently have the following code in my app which causes the view to load and then you see the keyboard appear (which isn't what I want, I want the keyboard to already be there)
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[theTextField becomeFirstResponder];
}
You can't put this in -viewWillAppear as the textfield hasn't been created yet so theTextField is nil. In the CoreDataBooks App where they achieve what i want they load their view from a nib so they use the same code but in -viewWillAppear as the textfield has already been created!
Is there anyway of getting around this without creating a nib, I want to keep the implementation programatic to enable greater flexibility.
Many Thanks
After speaking with the Apple Dev Support Team, I have an answer!
What you need to do is to create an offscreen UITextField in -(void)loadView; and then set it as first responder then on the viewDidLoad method you can set the UITextField in the UITableViewCell to be first responder. Heres some example code (remember I'm doing this in a UITableViewController so I am creating the tableview as well!
- (void)loadView
{
[super loadView];
//Set the view up.
UIView *theView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.view = theView;
[theView release];
//Create an negatively sized or offscreen textfield
UITextField *hiddenField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, -10, -10)];
hiddenTextField = hiddenField;
[self.view addSubview:hiddenTextField];
[hiddenField release];
//Create the tableview
UITableView *theTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStyleGrouped];
theTableView.delegate = self;
theTableView.dataSource = self;
[self.view addSubview:theTableView];
[theTableView release];
//Set the hiddenTextField to become first responder
[hiddenTextField becomeFirstResponder];
//Background for a grouped tableview
self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
//Now the the UITableViewCells UITextField has loaded you can set that as first responder
[theTextField becomeFirstResponder];
}
I hope this helps anyone stuck in the same position as me!
If anyone else can see a better way to do this please say.
Try do it in viewDidAppear method, works for me.
I think the obvious solution is to create the textfield in the init method of the view controller. That is usually where you configure the view because a view controller does require a populated view property.
Then you can set the textfield as first responder in viewWillAppear and the keyboard should be visible as the view slides in.
have you tried using the uinavigationcontroller delegate methods?:
navigationController:willShowViewController:animated:

Non scrolling tableViewHeader

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.

switch between uiviews with buttons and not uinavigation controllers

I have seen the post for How to switch views by buttons on iPhone? but this doesn't answer how to switch back and forth between views with buttons. The person that asked the question settled on the answer that they could switch between views with uinavigationcontroller.
I put the following code in an ibaction that kicks off when a button is pressed in the primary view.
PhoneNumberViewController *phoneNumberViewController1 = [[PhoneNumberViewController alloc] initWithNibName:#"PhoneNumberView2" bundle:nil];
self.phoneNumberViewController = phoneNumberViewController1;
[self.view removeFromSuperview];
[self.view insertSubview: phoneNumberViewController1.view atIndex:0];
When this code executes the whole view just goes blank. If I omit the removefromsuperview portion then the view disappears behind my button but the button still remains. I'm not sure if this is the right way to switch between buttons but if anyone knows how to do this please help. Also if anyone knows about any example projects that switch between views with buttons let me know.
Thanks a million!
You removed the view controller's view from it's superview and then added a subview to it. The view hierarchy is broken at the view controller's superview (likely your window). That is why you are getting a blank screen.
You'd likely want to keep a reference around to the original view and then swap it out to the new view by setting the view controller's view to the new view.
// origView is an instance variable/IBOutlet to your original view.
- (IBAction)switchToPhoneView:(id)sender {
if (origView == nil)
origView = self.view;
self.view = phoneViewController.view;
}
- (IBAction)switchToOriginalView:(id)sender {
self.view = origView;
}
The technique I usually use involves creating a superview class which contains a toolbar at the bottom, and a content view UIView class filling the rest of the screen.
I then add subviews to the content view to change the views based on button clicks. This approach makes it so the toolbar on the same is constant across all views. I start by defining a helper function like this:
-(void) clearContentView {
//helper to clear content view
for (UIView *view in [self.contentView subviews]){
[view removeFromSuperview];
}
}
My IBAction then looks like this:
-(IBAction) buttonClicked{
self.title = #"Images"; //change title of view
[self clearContentView]; //clear content view
[self.contentView addSubview:self.imagesViewController.view]; //add new view
[self.imagesViewController viewWillAppear:YES]; //make sure new view is updated
[self enableButtons]; //enable all other buttons on toolbar
self.imagesButton.enabled = NO; //disable currently selected button
}