UIButtons Text Appearing Darker While Changing Tabs in TabBar - iphone

I don't know why I am getting this problem.
Details - I have a tab-bar controller in my application. On one tab I have a form which contains some buttons. I am setting up the titles of these buttons. Now, when I change tabs and come back to the same tab, all the button titles appear darken.
I am attaching screen-shots as well.
Any help would be appreciated.
Thank you !!
EDIT : Here is the code of how I am creating the buttons -
-(void)viewWillAppear:(BOOL)animated
{
float yFrame =310.0f;
for(int i =0;i<7;i++){
openPickerButton=[UIButton buttonWithType:UIButtonTypeCustom];
openPickerButton.frame = CGRectMake(29.0, yFrame+10.0, 280.0, 48.0);
openPickerButton.tag=i;
openPickerButton.backgroundColor=[UIColor clearColor];
[openPickerButton setTitle:[formButtonTitle objectAtIndex:openPickerButton.tag] forState:UIControlStateNormal];
openPickerButton.titleLabel.font=[UIFont fontWithName:#"Helvetica" size:16];
[openPickerButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
openPickerButton.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft;
openPickerButton.showsTouchWhenHighlighted = YES;
[openPickerButton addTarget:self action:#selector(PickChoreButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[setPreferencesFormScrollView addSubview:openPickerButton];
yFrame+=60.0f;
}
}

Well in viewDidLoad your objects get called once.(As soon as the view get loaded.
Stop)
In viewWillAppear they get called every time the view appears.
- (void)viewDidLoad
{
[super viewDidLoad];
float yFrame =310.0f;
for(int i =0;i<7;i++){
openPickerButton=[UIButton buttonWithType:UIButtonTypeCustom];
openPickerButton.frame = CGRectMake(29.0, yFrame+10.0, 280.0, 48.0);
openPickerButton.tag=i;
openPickerButton.backgroundColor=[UIColor clearColor];
[openPickerButton setTitle:[formButtonTitle objectAtIndex:openPickerButton.tag] forState:UIControlStateNormal];
openPickerButton.titleLabel.font=[UIFont fontWithName:#"Helvetica" size:16];
[openPickerButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
openPickerButton.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft;
openPickerButton.showsTouchWhenHighlighted = YES;
[openPickerButton addTarget:self action:#selector(PickChoreButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[setPreferencesFormScrollView addSubview:openPickerButton];
yFrame+=60.0f;
}
}

Instead of creating buttons in viewWillAppear method create buttons in viewDidLoad method

Related

How to create multiline backBarButtonItem in UINavigationBar

I want to create multiline BackBarButtonItem. right now my back button display like this,
but i want to display it like this, color does not matter,
How can I do that? And this is my default back button which added while i am pushing my childviewcontroller thru my parentviewcontroller so i dont want to remove it.
You can do it using custom image,UILabel(MSLabel),UIButton.
MSLabel is used to change space between two line of UILabel. Because UILabel Does not provide any property to do that.
After you add MSLabel in your project use this code. Recommend size for backbtn.png is 48*30.
#import "MSLabel.h"
- (void)viewDidLoad
{
[super viewDidLoad];
// Set the custom back button
UIImage *buttonImage = [[UIImage imageNamed:#"backbtn.png"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 13, 0, 5)];
//create the button and assign the image
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
MSLabel *lbl = [[MSLabel alloc] initWithFrame:CGRectMake(12, 4, 65, 28)];
lbl.text = #"Account Summary";
lbl.backgroundColor = [UIColor clearColor];
//lbl.font = [UIFont fontWithName:#"Helvetica" size:12.0];
lbl.numberOfLines = 0;
lbl.lineHeight = 12;
lbl.verticalAlignment = MSLabelVerticalAlignmentMiddle;
lbl.font = [UIFont fontWithName:#"Helvetica" size:12];
[button addSubview:lbl];
//set the frame of the button to the size of the image (see note below)
button.frame = CGRectMake(0, 0, 70, buttonImage.size.height);
[button addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
//create a UIBarButtonItem with the button as a custom view
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customBarItem;
}
-(void)back {
NSLog(#"Dilip Back To ParentViewController");
// Tell the controller to go back
[self.navigationController popViewControllerAnimated:YES];
}
Outcome will display like this, Use your image for better result.
In this case you should use custom button and use background image on this button.Because i think it may not be possible.See for custom button
UIButton *backBtn = [[UIButton alloc] initWithFrame:CGRectMake(10.0f,6.5f,60.0f,30.0f)];
[backBtn setBackgroundImage:[UIImage imageNamed:#"accountSummary"] forState:UIControlStateNormal];
[backBtn setBackgroundImage:[UIImage imageNamed:#"accountSummary"] forState:UIControlStateHighlighted];
[backBtn addTarget:self action:#selector(accountSummaryBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
[self.navigationItem setLeftBarButtonItem:leftButton];
- (void)accountSummaryBtnPressed:(id)sender
{
//--------- do stuff for account summary
}
What i believe you can Create CustomView with ImageView And Transparent label with numberOflines more than two or whatever you want to restrict it to , then replace the navigation back button with That Barbutton initiated with CustomVIew, That Can Solve your problem,, Hope fully

How to disable UIButton highlight action

I'm trying to implement an analog of segmented control with two buttons. In default state they have no images, only labels, in selected one they have background image. I want to activate control with TouchDown event.
here is the code (I removed all unnecessary things):
-(IBAction) onButton1
{
button1.selected = YES;
button2.selected = NO;
}
-(IBAction) onButton2
{
button1.selected = NO;
button2.selected = YES;
}
the problem is: assume button1 is selected. When I touch button2 it does not change its image to "selected" image (there is no default image, as I've said), but when I release finger it changes. Also, if I touch already selected button it removes "selected" image and return it when I release it.
I've set highlighted state of buttons, so they have "selected" image in that state, but it did not help (not only in IB, but also with [button setBackgroundImage:[UIImage imageNamed:#"selected.png"] forState:UIControlStateHighlighted];). I've set adjustsImageWhenHighlighted = NO, that did not help too, again, in both program way and IB.
I've seen a lot of similar (but not identical) questions here, but they did not work for me.
Thanks in advance
Since, you wanted to activate the action at touch down event, I assume that you dont need highlighting. Try the following code
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.frame = CGRectMake(10.0f, 10.0f, 50.0f, 50.0f);
[button1 setBackgroundImage:[UIImage imageNamed:#"btn.jpeg"] forState:UIControlStateNormal];
[button1 setBackgroundImage:[UIImage imageNamed:#"btnsel.jpeg"] forState:UIControlStateSelected];
[button1 addTarget:self action:#selector(button1Action:) forControlEvents:UIControlEventTouchDown];
[button1 addTarget:self action:#selector(button1TouchUp:) forControlEvents:UIControlEventTouchUpInside|UIControlEventTouchUpOutside];
[self.view addSubview:button1];
button1.selected = YES;
button2 = [UIButton buttonWithType:UIButtonTypeCustom];
button2.frame = CGRectMake(80.0f, 10.0f, 50.0f, 50.0f);
[button2 setBackgroundImage:[UIImage imageNamed:#"btn.jpeg"] forState:UIControlStateNormal];
[button2 setBackgroundImage:[UIImage imageNamed:#"btnsel.jpeg"] forState:UIControlStateSelected];
[button2 addTarget:self action:#selector(button2Action:) forControlEvents:UIControlEventTouchDown];
[button2 addTarget:self action:#selector(button2TouchUp:) forControlEvents:UIControlEventTouchUpInside|UIControlEventTouchUpOutside];
[self.view addSubview:button2];
}
- (void)button1Action:(UIButton*)sender
{
button1.highlighted = NO;
button1.selected = YES;
button2.selected = NO;
}
- (void)button2Action:(UIButton*)sender
{
button2.highlighted = NO;
button2.selected = YES;
button1.selected = NO;
}
- (void)button1TouchUp:(id)sender
{
button1.highlighted = NO;
}
- (void)button2TouchUp:(id)sender
{
button2.highlighted = NO;
}

Add button to UITableViewCell

I want to add a button in a UITableViewCell. This is my code: `
if (indexPath.row==2) {
UIButton *scanQRCodeButton = [[UIButton alloc]init];
scanQRCodeButton.frame = CGRectMake(0.0f, 5.0f, 320.0f, 44.0f);
scanQRCodeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
scanQRCodeButton.backgroundColor = [UIColor redColor];
[scanQRCodeButton setTitle:#"Hello" forState:UIControlStateNormal];
[cell addSubview:scanQRCodeButton];
}`
Now, when I run the app, I see only a blank row ! Any ideas ?
While it's natural to put it in the contentView of the cell, I'm fairly certain that is not the problem (actually, in the past, I've never had subviews displayed correctly in the contentView, so I've always used the cell).
Anyway, the problem involves the first three lines of when you start creating your button. The first two lines are fine, but the code stops working with:
scanQRCodeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
buttonWithType: is actually a convenience method to create a button (it's like a compact alloc-init). Therefore, it actually "nullifies" your past two lines (you basically created the button twice). You can only use either init or buttonWithType: for the same button, but not both.
UIButton *scanQRCodeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
scanQRCodeButton.frame = CGRectMake(0.0f, 5.0f, 320.0f, 44.0f);
scanQRCodeButton.backgroundColor = [UIColor redColor];
[scanQRCodeButton setTitle:#"Hello" forState:UIControlStateNormal];
[cell addSubview:scanQRCodeButton];
This will work (note that you can use cell.contentView if you wanted). In case you're not using Automatic Reference Counting (ARC), I would like to mention that you don't have to do anything in term of memory management, because buttonWithType: returns an autoreleased button.
UIButton *deletebtn=[[UIButton alloc]init];
deletebtn.frame=CGRectMake(50, 10, 20, 20);
deletebtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[deletebtn setImage:[UIImage imageNamed:#"log_delete_touch.png"] forState:UIControlStateNormal];
[deletebtn addTarget:self action:#selector(DeleteRow:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:deletebtn];
or
// Download class and import in your project UIButton+EventBlocks
UIButton *deletebtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[deletebtn setFrame:CGRectMake(170,5, 25, 25)];
deletebtn.tag=indexPath.row;
[deletebtn setImage:[UIImage imageNamed:#"log_delete_touch.png"] forState:UIControlStateNormal];
[deletebtn setOnTouchUpInside:^(id sender, UIEvent *event) {
//Your action here
}];
[cell addSubview:deletebtn];
You want to add any custom UI elements to the cell's contentView.
So, instead of [cell addSubview:scanQRCodeButton];
do [cell.contentView addSubview:scanQRCodeButton];
Try adding [cell.contentView addSubview:scanQRCodeButton]; or if you want the button to the left side look at my question at the answer, to move the textLabel to the side. If you want the button to the right then just set it as your accesoryView like this cell.accesoryView = scanQRCodeButton;.
Method setTitle does not work in my code, so I have set by using
[UIButton.titleLabel setText:#""]
instead of using setTitle method.
Please try again with following code:
[scanQRCodeButton.titleLabel setText:#"Hello"];
Then it would work well.

Putting a button in table footer view

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. :-)

section header in uitableview - reusidentifier?

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.