How to add a button on top off viewForHeaderInSection. I like to add a button above section header which can be scrollable with tableview. Any idea how to do this ?
The - rather hackish - solution I saw in a pull-to-refresh implementation is to simply add your 'extra' view to the table view as a subview - just make sure it's positionned using a negative y offset. That offset should be equal to (well, rather -1 times) the height of your view. Code:
UIView *myViewAboveHeader = // however you create it
CGRect f = myViewAboveHeader.frame;
f.origin.y = -1 * f.size.height;
myViewAboveHeader.frame = f;
[tableView addSubview:myViewAboveHeader];
Edit: it seems you don't want a header view AND a view above it. In this case, just simply add a button on top of the view you return in your table view delegate method:
- (UIView *)tableView:(UITableView *)tv viewForHeaderInSection:(NSInteger)s
{
UIView *header = ...;
UIButton *btn = // create a button somehow;
[header addSubview:btn];
return header;
}
Use this code, placing it in viewdidload:
- (void)viewDidLoad {
UIView *headerViews = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 90)];
UIButton *managePrivacyButton = [UIButton buttonWithType:UIButtonTypeCustom];
[managePrivacyButton setFrame:CGRectMake(0, 45, 320, 45)];
managePrivacyButton.titleLabel.textAlignment = UITextAlignmentLeft;
[managePrivacyButton.titleLabel setFont:[UIFont boldSystemFontOfSize:12]];
[managePrivacyButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[headerViews addSubview:managePrivacyButton];
[self.tableView setTableHeaderView:headerViews];
[super viewDidLoad];
}
This method in particular takes as a return an UIView. UIButton is a subclass of UIView. Just create a new UIButton and return it.
Related
I'm trying to create a UITableView with multiple sections. Each section has it's own header, but I am trying to make a universal footer for the entire table view that stays in one position...
Is this logic possible using the UITableViewDelegate methods? Or should I create a custom view and just try and add it as a subview to my table view? The footer I currently have contains a UIButton.
If anyone has some sample code that would be great.
Edit: This question is not the same as the one referenced. I am trying to make a universal footer that floats above the UITableView. The other question does not specify the location of the footer, only that a footer is desired.
read about UITableView's tableFooterView property.
And now some code: (using ARC)
UILabel *footer = [UILabel alloc] init];
footer.text = #"Some text" ;
footer.backgroundColor = [UIColor greenColor];
self.tableView.tableFooterView = footer;
Now at the bottom of entire tableview there is a UILabel that is green with some text.
I ended up creating a subview and adding my button to that view. I then made that view my "footer".
Here is the code that gave me the desired results.
- (void)viewDidLoad
{
[super viewDidLoad];
//self.tableView.delegate = self;
//self.tableView.dataSource = self;
//save current tableview, then replace view with a regular uiview
self.tableView = (UITableView*)self.view;
UIView *replacementView = [[UIView alloc] initWithFrame:self.tableView.frame];
self.view = replacementView;
[self.view addSubview:self.tableView];
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 370, 320, 45)];
//create the button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//button.userInteractionEnabled = YES;
//the button should be as big as a table view cell
//width of the button can be set but the width of the view it is added to will always match the width of the tableView
[button setFrame:CGRectMake(60, 0, 200, 45)];
//set title, font size and font color
[button setTitle:#"Build" forState:UIControlStateNormal];
[button.titleLabel setFont:[UIFont boldSystemFontOfSize:20]];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//set action of the button
[button addTarget:self action:#selector(buildThenSegue)
forControlEvents:UIControlEventTouchUpInside];
//add the button to the view
[footerView addSubview:button];
footerView.userInteractionEnabled = YES;
[self.view addSubview:footerView];
self.tableView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
}
Take note that this is in a subclass of UITableViewController.
I referenced this answer to another question: https://stackoverflow.com/a/9084267/1091868
So, I have implemented this method to add a footer to my table view:
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 60;
}
This is the result:
Now, you see, there are two problems:
1) I would like to add a button inside the table view footer but when I drag a button control in the storyboard, it does not work. How to add a button there?
2) As you can see, the footer is transparent and there is a table view cell visible under it. I would like there to be no cells under the footer (so the last visible cell would be the one above the footer). Second, I would like the footer not to be transparent.
I am using Xcode 4.2 and Snow Leopard.
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
is a delegate method. Try instead to access sectionFooterHeight property of your table.
To add a button you may consider adding a custom view to your footer. You can access tableFooterView and assign a custom view to it.
Use this function to create a footer view for table
- (UIView *) createViewForTableFooter
{
CGRect footerRect = CGRectMake(0, 0, self.view.frame.size.width, 60);
UIView *tableFooter = [[[UIView alloc] initWithFrame:footerRect] autorelease];
tableFooter.backgroundColor = [UIColor clearColor];
UIButton* verifyButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[verifyButton setTitle:WNLocalizedString(#"Verify",#"") forState:UIControlStateNormal];
[verifyButton.titleLabel setFont:[UIFont boldSystemFontOfSize:20]];
[verifyButton addTarget:self action:#selector(verifyBtnTapped:)forControlEvents:UIControlEventTouchUpInside];
verifyButton.autoresizingMask = UIViewAutoresizingFlexibleWidth;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
[verifyButton setFrame:CGRectMake(44, 10, self.view.frame.size.width - 88, 37)];
}else {
[verifyButton setFrame:CGRectMake(10, 10, self.view.frame.size.width - 20, 37)];
}
[tableFooter addSubview:verifyButton];
return tableFooter;
}
And use this function like this in your viewDidLoad method
yourTableObjectName.tableFooterView = [self createViewForTableFooter];
you can use tableView:viewForFooterInSection: method to add button to the footer
increase contente size of tableviews bottom
UIEdgeInsets contentInset = self.tableView.contentInset;
contentInset.bottom = 50.0f; // **the height of the footer**
self.tableView.contentInset = contentInset;
I'm using two UIButtons in a footer of a UITableView (both subviews of a single UIView). I am not using a UITableViewCell because I wanted to skin the button so it looks like the red Delete button at the bottom of some iPhone screens such as when editing a contact.
It is sized and works correctly. However, the table will resize itself on device orientation changes (landscape, portrait and so on) and the button stays its original width. I tried using autoresizing masks but nothing worked.
Is there a trick to it, or a better way?
It should work with autoresizingmasks, I've done it before but it's important to set the width of your view correctly and add the correct sizingmasks.
Some sample code to show how it works. This creates two buttons resizing whem you rotate.
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 50)];
view.backgroundColor = [UIColor redColor];
UIButton *buttonA = [UIButton buttonWithType:UIButtonTypeRoundedRect];
buttonA.frame = CGRectMake(20, 5, 125, 40);
[buttonA setTitle:#"ButtonA" forState:UIControlStateNormal];
buttonA.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
[view addSubview:buttonA];
UIButton *buttonB = [UIButton buttonWithType:UIButtonTypeRoundedRect];
buttonB.frame = CGRectMake(175, 5, 125, 40);
[buttonB setTitle:#"ButtonB" forState:UIControlStateNormal];
buttonB.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
[view addSubview:buttonB];
return [view autorelease];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 50;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return YES;
}
Please use 2 frames for button one for landscape mode and another for portrait mode..this will work out..so when orientatin changes check whetehr its landscape or portrait and assign frame for it...check mode here
willrotatetointerfaceorientation
Best i've come up with is, use one button and one cell in its own section, then each will resize appropriately.
I do something very similar to yours except I only have one button. the uiview that you are returning should be the same width as the tableView itself.
CGFloat footerWidth = mainTableView.frame.size.width;
CGRect frameTableFooter = CGRectMake(0.0, 0.0, footerWidth, 60.0);
viewForTableFooter.frame = frameTableFooter;
// buttons addition
mainTableView.tableFooterView = viewForTableFooter;
I want to add a table header (not section headers) like in the contacts app for example:
exactly like that - a label beside an image above of the table.
I want the all view be scrollable so I can't place those outside of the table.
How can I do that?
UITableView has a tableHeaderView property. Set that to whatever view you want up there.
Use a new UIView as a container, add a text label and an image view to that new UIView, then set tableHeaderView to the new view.
For example, in a UITableViewController:
-(void)viewDidLoad
{
// ...
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(XXX, YYY, XXX, YYY)];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(XXX, YYY, XXX, YYY)];
[headerView addSubview:imageView];
UILabel *labelView = [[UILabel alloc] initWithFrame:CGRectMake(XXX, YYY, XXX, YYY)];
[headerView addSubview:labelView];
self.tableView.tableHeaderView = headerView;
[imageView release];
[labelView release];
[headerView release];
// ...
}
You can do it pretty easy in Interface Builder. Just create a view with a table and drop another view onto the table. This will become the table header view. Add your labels and image to that view. See the pic below for the view hierarchy.
In Swift:
override func viewDidLoad() {
super.viewDidLoad()
// We set the table view header.
let cellTableViewHeader = tableView.dequeueReusableCellWithIdentifier(TableViewController.tableViewHeaderCustomCellIdentifier) as! UITableViewCell
cellTableViewHeader.frame = CGRectMake(0, 0, self.tableView.bounds.width, self.heightCache[TableViewController.tableViewHeaderCustomCellIdentifier]!)
self.tableView.tableHeaderView = cellTableViewHeader
// We set the table view footer, just know that it will also remove extra cells from tableview.
let cellTableViewFooter = tableView.dequeueReusableCellWithIdentifier(TableViewController.tableViewFooterCustomCellIdentifier) as! UITableViewCell
cellTableViewFooter.frame = CGRectMake(0, 0, self.tableView.bounds.width, self.heightCache[TableViewController.tableViewFooterCustomCellIdentifier]!)
self.tableView.tableFooterView = cellTableViewFooter
}
You can also simply create ONLY a UIView in Interface builder and drag & drop the ImageView and UILabel (to make it look like your desired header) and then use that.
Once your UIView looks like the way you want it too, you can programmatically initialize it from the XIB and add to your UITableView. In other words, you dont have to design the ENTIRE table in IB. Just the headerView (this way the header view can be reused in other tables as well)
For example I have a custom UIView for one of my table headers. The view is managed by a xib file called "CustomHeaderView" and it is loaded into the table header using the following code in my UITableViewController subclass:
-(UIView *) customHeaderView {
if (!customHeaderView) {
[[NSBundle mainBundle] loadNibNamed:#"CustomHeaderView" owner:self options:nil];
}
return customHeaderView;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Set the CustomerHeaderView as the tables header view
self.tableView.tableHeaderView = self.customHeaderView;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0,tableView.frame.size.width,30)];
headerView.backgroundColor=[[UIColor redColor]colorWithAlphaComponent:0.5f];
headerView.layer.borderColor=[UIColor blackColor].CGColor;
headerView.layer.borderWidth=1.0f;
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5,100,20)];
headerLabel.textAlignment = NSTextAlignmentRight;
headerLabel.text = #"LeadCode ";
//headerLabel.textColor=[UIColor whiteColor];
headerLabel.backgroundColor = [UIColor clearColor];
[headerView addSubview:headerLabel];
UILabel *headerLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(60, 0, headerView.frame.size.width-120.0, headerView.frame.size.height)];
headerLabel1.textAlignment = NSTextAlignmentRight;
headerLabel1.text = #"LeadName";
headerLabel.textColor=[UIColor whiteColor];
headerLabel1.backgroundColor = [UIColor clearColor];
[headerView addSubview:headerLabel1];
return headerView;
}
I'm doing an iPhone ap which contains a viewController class (PhotoListViewController). This viewController's view has a subview which contains a button attached as a subview. I'm trying to use the addTarget:action:forControlEvent method to call an instance method of the PhotoListViewController class in order to perform an action when the button is pressed but the method is not being called. Any thoughts on how to make this work?
Here's the pertinent code. I know your first though will be why am I nesting subviews, but this just a distilled version of my program - there are reasons I'm doing the nesting. I know there are probably other ways of setting up the program so I don't have to do this nesting, but my real question is - why does this not work?
/Code/
#implementation PhotoListViewController
- (void)loadView {
[super viewDidLoad];
//load elements into the viewController
//first create the frame
CGRect frameParent = CGRectMake(0, 0, 0, 0);
UIView *viewPerson = [[UIView alloc] initWithFrame:frameParent];
CGRect frameChild = CGRectMake(0, 20, 0, 0);
UIView *newView = [[UIView alloc] initWithFrame:frameChild];
UIButton *btnShow = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnShow.frame = CGRectMake(230, 120, 80, 30);
[btnShow setTitle:#"View!" forState:UIControlStateNormal ];
[btnShow addTarget:self
action:#selector(viewPhoto_click:)
forControlEvents:UIControlEventTouchUpInside];
[newView addSubview:btnShow];
[viewPerson addSubview:newView];
[newView release];
[self setView:viewPerson];
[viewPerson release];
}
- (void)viewPhoto_click:(UIButton*)sender {
NSLog(#"HI");
}
It may not be related, but both viewPerson and newView have a width and height of 0. You may want to give these views a non-null size (the 3rd and 4th arguments of CGRectMake).
action:#selector(viewPhoto_click:)
forControlEvents:UIControlEventTouchUpInside];
May be a copy paste typo but have you tried to remove the : after the viewPhoto_click after the selector?
Also. In most examples the event Handler look like this.
- (void) viewPhoto_click: (id)sender
Not sure if it makes any difference though