I am creating a custom button and putting it in my table's footer view but the button is going way out from the right corner.
What is wrong here!?!
Here is my code and output image:
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[aButton setTitleColor:[UIColor colorWithWhite:0.0 alpha:0.56] forState:UIControlStateDisabled];
[aButton setBackgroundImage:[[UIImage imageNamed:#"test.png"] stretchableImageWithLeftCapWidth:kButtonSliceWidth topCapHeight:0] forState:UIControlStateNormal];
[aButton setTitle:#"Click me" forState:UIControlStateNormal];
[aButton.titleLabel setFont:[UIFont boldSystemFontOfSize:kFontSize14]];
[aButton setFrame:CGRectMake(10.0, 15.0, 300.0, 44.0)];
[self.tableView setTableFooterView:aButton];
I added the button in the UIView and then set the tableFooterView to this UIView object and it worked. We cannot directly put UIButton as tableFooterView.
Try doing like this. Set IBOutlet UIView *footerView; and synthesize it. Add it as a subview using the method-
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
if(footerView == nil) {
//allocate the view if it doesn't exist yet
footerView = [[UIView alloc] init];
//create the button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(10, 3, 300, 44)];
//set title, font size and font color
[button setTitle:#"Click Me" forState:UIControlStateNormal];
[button.titleLabel setFont:[UIFont boldSystemFontOfSize:18]];
//set action of the button
[button addTarget:self action:#selector(clickPressed:)
forControlEvents:UIControlEventTouchUpInside];
//add the button to the view
[footerView addSubview:button];
}
//return the view for the footer
return footerView;
}
One important addition to Legolas' fine answer: You'll need to implement the tableView delegate method:
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
foot 50;
}
This was driving me nuts because the button (or, in my case, buttons) were not responding to the touch event. Adding the delegate method made it all better. :-)
Related
I am new to iOS development and I have a problem with UIScrollView. First of all, I've created a Scroll View in a storyboard and added
#property (retain, nonatomic) IBOutlet UIScrollView *mainScroll;
in view controller handler
In the viewDidLoad method I have the following code:
- (void)viewDidLoad
{
[super viewDidLoad];
self.mainScroll.contentSize = CGSizeMake(100.0,100.0); // Have to set a size here related to your content.
// You should set these.
self.mainScroll.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.mainScroll.minimumZoomScale = .5; // You will have to set some values for these
self.mainScroll.maximumZoomScale = 2.0;
self.mainScroll.zoomScale = 1;
UIButton* b = [[UIButton alloc] init];
[b setBounds:CGRectMake(.0f,0.f,100.0,100.0)];
[self.mainScroll addSubview:b];
}
But the button does not appear in the simulator. However if I add a button on storyboard in the IB, the button appears and I can scroll the view.
How can I add the button in code?
Try this code
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.mainScroll addSubview:button];
bounds and frame are not the same thing.
Use this code instead...
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Hello" forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, 100, 44);
[self.mainScroll addSubview:button];
You need to set scroll view contents size, something like the following code.
[self.mainScroll setContentSize:CGSizeMake(width, height)];
Try this :
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Hello" forState:UIControlStateNormal];
button.frame = CGRectMake(self.mainScroll.frame.origin.x + 25 ,self.mainScroll.frame.origin.y +25, 50, 44);
[self.mainScroll addSubview:button];
Best and easiest way to make a button from code is:
UIButton* yourButton = [[UIButton alloc]initWithFrame:CGRectMake(x, y, width, height)];
[yourButton setTitle:#"Your Title" forState:UIControlStateNormal];
// if you set the button's image the title won't be visible, because button's title is "under" the image. I recomend you to use this if you have a cut and dried image.
[yourButton setImage:[UIImage imageNamed:#"yourImage.png"] forState:UIControlStateNormal];
// if you set the button's background image, button's title will be visible
[yourButton setBackgroundImage:[UIImage imageNamed:#"yourBackgroundImage.png"] forState:UIControlStateNormal];
// you can add target to the button, for handle the event when you touch it.
[yourButton addTarget:self action:#selector(handleToucUpInside:) forControlEvents:UIControlEventTouchUpInside];
// then add the button
[self.view addSubview:yourButton];
- (void) handleToucUpInside:(id)sender{
// here you can do anything you want, to handle the action
}
I am trying to add button into my table cell like below:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
....
UIButton* buttonCheckbox = [UIButton buttonWithType:UIButtonTypeCustom];
buttonCheckbox.frame = CGRectMake(0, 177, 56, 26);
buttonCheckbox.backgroundColor = [UIColor redColor];
[buttonCheckbox addTarget:self action:#selector(actionFirst1) forControlEvents:UIControlEventTouchUpInside];
[buttonCheckbox setTitle:#"MyTitle" forState:UIControlStateNormal];
[cell addSubview:buttonCheckbox];
but actionFirst1 event is not fired. if i add the button [self.view addSubview:buttonCheckbox] instead of [cell addSubview:buttonCheckbox] it works fine. why?
thanks...
You should add this button to the contentView property of your cell
[cell.contentView addSubview:buttonCheckbox];
In fact, all your custom elements inside a UITableViewCell should be placed inside the contentView as this cell elements (contentView, accessorView and image) are all placed on top of any elements that your cell have.
Check this doc for more info on cell about custom UITableviewCell
Hi try below code it may help you out:
if ([indexPath row] == 0)
{
UIButton *myButton = [[UISwitch alloc]initWithFrame:CGRectMake(x, y, width,
heigth)];
[cell addSubview:myButton];
cell.accessoryView = myButton;
[myButton addTarget:self action:#selector (actionFirst1)
forControlEvents:UIControlEventTouchUpInside];
}
This code will help you out. Please set frames according to your cell of TableView.
Please see whether it would suffice to add your button as the accessoryView of the table cell. In that case, the iOS framework will position the button in the right place for your cell style itself (though you still need to give it a size). Here's an example from one of my projects:
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(0, 0, 100, 27); // only size matters
[myButton setTitle:#"Connect" forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(connect:) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryView = myButton;
That's it. You just create the control, make sure it has a size, and set it to the accessoryView.
I have a UITableView with three section. I have a custom UITableViewCell. I want to put a button in the first section of my table view to handle an action.
How to can do this please?
just create the button programmatically
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];
You can also put the button directly into a UITableViewCell which is located in your first section. In your cellForRowAtIndexPath you can use for example:
UIButton *yourButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[yourButton setTitle:#"Click me" forState:UIControlStateNormal];
[yourButton setFrame: CGRectMake( 20.0f, 3.0f, 280.0f, 33.0f)];
[yourButton addTarget:self action:#selector(clickAction) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:yourButton];
This example will look like this:
and will be places directly in the cell.
If you want to put it in the section header (rather than a cell), you could:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if(section==0)
{
// create button and return it
}
}
Im new to iphone development.here i added some list of names in pickerview.the action of pickerview was given to button in view. now i want to show.when click the button pickerview list was displayed i selected one name regarding in pickerview that name was displayed at button in view. I dont no how to change the title in button in iphone.
Can any one plz give me information for my problem.
thank you in advance.
You should be able to change the title of a UIButton by calling setTitle. Just remember to set it for all the states of the IUIButton. Here is an example:
[button setTitle:#"Normal Title" forState:UIControlStateNormal];
[button setTitle:#"Highlighted Title" forState:UIControlStateHighlighted];
declare your button and make property of your button in .h file
UIButton *btnl;
#property(nonatomic,retain) IBOutlet UIButton *btnl;
and make connection from IB.
Now in .m file,
use this delegate method of you picker
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
btn.text=[yourArrayOfPickerContent objectAtIndex:row];
}
To change the custom button title use this:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(82,203,129,45);
[button setTitle:#"btnTitle" forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
[self.view bringSubviewToFront:button];
To change button title,
button.titleLabel.text=#"String";
You should Code in the pickerView delegate
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
This way you can code about UIButton and can set the title to it.
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0.0f, 4.0f, 90.0f, 20.0f)];
[btn setBackgroundColor:[UIColor clearColor]];
UIImage *backgroundView = [UIImage imageNamed:#"btn.png"];
[btn setBackgroundImage:backgroundView forState:UIControlStateNormal];
[btn setTitle:#"buttonName" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
first you need to create a pointer of type UIButton as IBOutlet in your view controller and than connect that button to the one in IB.
when you want to change the name of title just do
button.titleLabel.text = [NSString stringWithString:yourString];
I have a table with customized headers for the sections. In addition to that, I implemented a collapsing feature, so that when a header is touched, all non-adressed sections are reduced, and the adressed section is expanded.
I realized that by using UIButtons as background views. Furthermore, the buttons have different colors and text, depending on their state (expanded vs not expanded).
I have a problem, similar to that reusidentifier problem, i.e. if u dont reuse cells already allocated in a table, certain fragments appear, if u start to scroll. Here it only happens to my first section, nevertheless it appears fragmented and duplicated....
Is there anything similar to reusing headerviews already allocated once like the method reusIdentifier for UITableViewCells?
Or is there a better approach?
Allocating the Buttons in advance to store them in array didnt do the trick for me...
Here's some code:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 40)];
[button setShowsTouchWhenHighlighted:NO];
[button setTitleShadowColor:[UIColor clearColor] forState:UIControlStateNormal];
[button addTarget:self action:#selector(collapse:) forControlEvents:UIControlEventTouchUpInside];
button.tag = section;
[button setAdjustsImageWhenHighlighted:NO];
button.backgroundColor = [UIColor colorWithRed:63.0f/255.0f green:154/255.0f blue:201/255.0f alpha:1.0f];
[button.titleLabel setFont:[UIFont fontWithName:#"Helvetica" size:14.0]];
[button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
[button setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
if (section == 0) {
// [button setBackgroundImage:[UIImage imageNamed:#""] forState:UIControlStateNormal];
[button setTitle:#" Newsroom" forState:UIControlStateNormal];
} else {
[button setTitle:[NSString stringWithFormat:#" Sektion %d", section] forState:UIControlStateNormal];
}
if (section == mySection) {
[button setBackgroundImage:[UIImage imageNamed:#"sectionHeader_active.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"sectionHeader_active.png"] forState:UIControlEventTouchUpInside];
[button.titleLabel setTextColor:[UIColor whiteColor]];
} else {
[button setBackgroundImage:[UIImage imageNamed:#"sectionHeader_inactive.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"sectionHeader_inactive.png"] forState:UIControlEventTouchUpInside];
[button.titleLabel setTextColor:[UIColor colorWithRed:96.0f/255.0f green:96/255.0f blue:97/255.0f alpha:1.0f]];
}
return [button autorelease];
}
I also implemented a collapsing section table using UIButton header views. What I found was that it was folly trying to cache and resuse the views, because you don't know when the UITableView is done with them (I got crashes).
I recommend implementing your state change by: updating your data model; performing all the row insert/deletes to perform the collapses/expand; then doing a [tableView reloadData], which will call viewForHeaderInSection, where you use the state of your data model to return the appropriately formatted uibutton view.