UIButton set title color not working - ios

I have created the array of UIButtons , having the same action,
if i click the first button, the first button Colour should be change, other button Colour should not be changed. how to achieve this?
for(titleStr in titleArray){
actionButton = [UIButton buttonWithType:UIButtonTypeCustom];
[actionButton addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
actionButton.tag = count;
[actionButton setTitle:titleArray[count] forState:UIControlStateNormal];
[actionButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[actionButton setBackgroundColor:[UIColor clearColor]];//[UIColor greenColor]]; // AJS 3.3 change
CGSize expectedTitleSize = [actionButton.titleLabel sizeThatFits:maximumLabelSize];
CGRect buttonframe;
buttonframe=CGRectMake(contentOffset,0, expectedTitleSize.width+5.0, expectedTitleSize.height+25);
actionButton.frame = buttonframe;
[titlewidthArray addObject:[NSNumber numberWithFloat:expectedTitleSize.width]];
[contentoffsetArray addObject:[NSNumber numberWithFloat:contentOffset+3.0]];
if(count==0){
actionButton.titleLabel.font=[UIFont fontWithName:#"HelveticaNeue-Medium" size:15.0];
[actionButton setSelected:YES];
tempObj = actionButton;
}else {
actionButton.titleLabel.font=[UIFont fontWithName:#"HelveticaNeue-Medium" size:15.0];
}
[titleScrollView addSubview:actionButton];
contentOffset += actionButton.frame.size.width+5.0;
titleScrollView.contentSize = CGSizeMake(contentOffset, titleScrollView.frame.size.height);
// Increase the count value
count++;
}
-(void)buttonTapped:(id)sender {
if([sender tag]==0){
UIButton *tappedButton = (UIButton *)sender;
[actionButton setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
actionButton.titleLabel.textColor = [UIColor whiteColor];
[actionButton setTitleColor:[UIColor whiteColor] forState:UIControlStateDisabled];
[tappedButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}
}
Thanks advance

for that you have to take one extra UIButton Object in you header file.
While user click on any button you have to store that selected Button in refbutton object and use like below.
#interface YourViewController ()
{
UIButton *btnSelected;
}
Now Move to your Button Action:
-(IBAction)buttonTapped:(UIButton *)sender {
if(_btnSelected){
_btnSelected.titleLabel.textColor = [UIColor blackColor];
}
_btnSelected = sender;
sender.titleLabel.textColor = [UIColor whiteColor];
}
Hope this will help you.

Related

Select/Deselect for dynamically created UIButtons

I will explain the Scenario:
I am dynamically creating three UIButtons inside a for loop.The condition is by default the first button should be bordered.Now if we select other buttons the previous button's border should go and now the border should be present for selected button.
for (int btnsCount = 0 ; btnsCount <[DataList count]; btnsCount++) {
self.graphSubDataButton = [[UIButton alloc] init];
self.graphSubDataButton.frame = CGRectMake(buttonsOffset, 0, 120, 40);
[self.graphSubDataButton setTitleColor:UIColorFromRGB(0x548DCD) forState:UIControlStateNormal];
[self.graphSubDataButton.titleLabel setFont:[UIFont fontWithName:AVENIR_NEXT_MEDIUM size:13.0f]];
self.graphSubDataButton.tag = btnsCount+1;
[self.graphSubDataButton addTarget:self action:#selector(enableRespectiveButton2:) forControlEvents:UIControlEventTouchUpInside];
buttonsOffset = buttonsOffset+30 + self.graphSubDataButton.frame.size.width ;
if (self.graphSubDataButton.tag==1)
{
[self.graphSubDataButton.layer setBorderWidth:1.2f];
[self.graphSubDataButton.layer setBorderColor:[UIColorFromRGB(0x3070BA) CGColor]];
[self.graphSubDataButton.layer setCornerRadius:8.0f];
}
}
-(void) enableRespectiveButton2:(UIButton *) sender
{
}
I have changed your code structure little to make it work properly, you can comment if you don't understand something.
NSInteger defaultSelectedTag = 1;
for (int btnsCount = 0 ; btnsCount <[DataList count]; btnsCount++) {
UIButton *graphSubDataButton = [UIButton buttonWithType:UIButtonTypeCustom];
graphSubDataButton.frame = CGRectMake(buttonsOffset, 0, 120, 40);
[graphSubDataButton setTitleColor:UIColorFromRGB(0x548DCD) forState:UIControlStateNormal];
[graphSubDataButton.titleLabel setFont:[UIFont fontWithName:AVENIR_NEXT_MEDIUM size:13.0f]];
graphSubDataButton.tag = btnsCount+1;
[graphSubDataButton addTarget:self action:#selector(enableRespectiveButton:) forControlEvents:UIControlEventTouchUpInside];
buttonsOffset = buttonsOffset+30 + self.graphSubDataButton.frame.size.width ;
//add your button somewhere
//e.g.
//[self.view addSubview:graphSubDataButton];
if (graphSubDataButton.tag == defaultSelectedTag) {
[self setBorderForButton:graphSubDataButton];
}
}
- (void) setBorderForButton:(UIButton *)sender {
[sender.layer setBorderWidth:1.2f];
[sender.layer setBorderColor:[UIColorFromRGB(0x3070BA) CGColor]];
[sender.layer setCornerRadius:8.0f];
}
- (void) enableRespectiveButton2:(UIButton *) sender {
for(UIButton *buttons in [sender.superview subviews]) {
if([buttons isKindOfClass:[UIButton class]]) {
if(![buttons isEqual:sender]) {
//add logic to, remove borders
//buttons.layer.borderWidth = 0.0f;
}
}
}
[self setBorderForButton:sender];
}
Use tag to get current selected button or much simple: iterate through all subviews and set border to 0 for UIButtons
for (UIView *subview in [uiv_ButtonsView subviews]) {
if([subview isKindOfClass:[UIButton class]]) {
subview.layer setBorderWidth:0.0f;
}
}
then use:
UIButton *button=(UIButton *)sender;
[button.layer setBorderWidth:1.2f];
[button.layer setBorderColor:[UIColorFromRGB(0x3070BA) CGColor]];
[button.layer setCornerRadius:8.0f];
Create dynamic buttons like :
int buttonsOffset = 50;
for (int btnsCount = 0 ; btnsCount <3; btnsCount++)
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.backgroundColor = [UIColor blueColor];
btn.frame = CGRectMake(buttonsOffset, 100, 120, 40);
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[btn.titleLabel setFont:[UIFont systemFontOfSize:13.0f]];
btn.tag = btnsCount+1;
[btn addTarget:self action:#selector(enableRespectiveButton2:) forControlEvents:UIControlEventTouchUpInside];
buttonsOffset = buttonsOffset+30 + btn.frame.size.width;
if (btn.tag==1)
{
[btn.layer setBorderWidth:1.2f];
[btn.layer setBorderColor:[UIColor greenColor].CGColor];
[btn.layer setCornerRadius:8.0f];
self.graphSubDataButton = btn;
}
[self.view addSubview:btn];
}
And click event of button is as following
- (void)enableRespectiveButton2:(UIButton *)sender
{
sender.selected = TRUE;
[sender.layer setBorderWidth:1.2f];
[sender.layer setBorderColor:[UIColor greenColor].CGColor];
[sender.layer setCornerRadius:8.0f];
self.graphSubDataButton.selected = FALSE;
[self.graphSubDataButton.layer setBorderWidth:0.0f];
[self.graphSubDataButton.layer setBorderColor:[UIColor clearColor].CGColor];
[self.graphSubDataButton.layer setCornerRadius:0.0f];
self.graphSubDataButton = sender;
}
Create dynamic buttons with for loop as shown below.
for(int i = 0 ;i <3; i++) {
UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
NSString * title = [NSString stringWithFormat:#"Button %d",i];
[btn setTitle:title forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:#"BorderImg"] forState:UIControlStateSelected];
[btn setBackgroundImage:[UIImage imageNamed:#"UnBordered"] forState:UIControlStateNormal];
if(i == 0) [btn setSelected:YES];
else [btn setSelected:NO];
[btn setTag:i];
[btn addTarget:self action:#selector(btnTapped:) forControlEvents:UIControlEventTouchUpInside];
}
Have 2 images
Highlighted border (BorderImg)
Normal border (UnBordered)
Set those images to button while creating in for loop
- (void)btnTapped:(UIButton *) btn {
if(! btn.isSelected) {
[btn setSelected:YES];
}
Inside button selector method, based on the sender selection state , update the button selected state. (In side selector method i.e btnTapped set other buttons selected state to false)

iOS bordered button behaviour on tap

I need a pre-iOS UIButton with border in my app. I use:
btn.layer.borderColor = [UIColor blackColor].CGColor;
btn.layer.borderWidth = 1.0;
btn.layer.cornerRadius = 10;
This works ok but on tap the border does not flash(button highlight state). Any way to easily implement this ?
Implement this when creating the button:
[self.botonConfigurar addTarget:self action:#selector(setBgColorForButton:) forControlEvents:UIControlEventTouchDown];
[self.botonConfigurar addTarget:self action:#selector(clearBgColorForButton:) forControlEvents:UIControlEventTouchDragExit];
[self.botonConfigurar addTarget:self action:#selector(setBgColorForButton:) forControlEvents:UIControlEventTouchDragEnter];
and these 2 functions:
-(void)setBgColorForButton:(UIButton*)sender {
[self.botonConfigurar setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
self.botonConfigurar.layer.backgroundColor = [UIColor colorWithRed:89/255.0 green:194/255.0 blue:237/255.0 alpha:1.0].CGColor;
}
-(void)clearBgColorForButton:(UIButton*)sender {
[self.botonConfigurar setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
self.botonConfigurar.layer.backgroundColor = [UIColor colorWithRed:61/255.0 green:169/255.0 blue:222/255.0 alpha:1].CGColor;
}
Do whatever you want with the button inside these functions.
PD: Make your button a [UIButton buttonWithType:UIButtonTypeCustom]
PD2: In clearBgColorForButton:sender write your UIButton's original parameters.
This is Just Example
[button setBackgroundImage:[self imageWithColor:[UIColor blueColor]] forState:UIControlStateHighlighted];

How to change title color and background color of dynamic UIButton when clicked and first button should be selected as default?

I have a view and I created some 8 dynamic buttons programmatically in that. The title color of buttons is white color. I want to change color of button title to green color when it is clicked. And if i click any other button, the previously clicked button title color should become white and current button title color should become green.
I have used the following code and its working properly for changing the color of previous button and now I want the first button to be in selected mode as default and first button should be changed when another button is clicked.
- (void)viewDidLoad
{
_examsListDict1 = [[NSMutableDictionary alloc]init];
_examsListDict1[#"Exam Name"] = #"JEE Mains";
.........so on another 4 dictionaries
_examsListArray = [[NSMutableArray
alloc]initWithObjects:_examsListDict1,_examsListDict2,_examsListDict3,_examsListDict4,_examsListD
ict5, nil];
NSUInteger i;
int xCoord=0;
int yCoord=0;
int buttonWidth=120;
int buttonHeight=50;
int buffer = 1;
for (i = 0; i <[_examsListArray count]; i++)
{
aButton = [UIButton buttonWithType:UIButtonTypeCustom];
aButton.frame = CGRectMake(xCoord, yCoord,buttonWidth,buttonHeight );
[aButton setTitle:[[_examsListArray objectAtIndex:i] objectForKey:#"Exam Name"]
forState:UIControlStateNormal];
[aButton setTag:i];
[aButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[aButton setBackgroundColor:[UIColor colorWithRed:250.0f/255.0f green:255.0f/255.0f
blue:221.0f/255.0f alpha:1]];
[aButton addTarget:self action:#selector(whatever:)
forControlEvents:UIControlEventTouchUpInside];
[_buttonsHorizontalScrollView addSubview:aButton];
xCoord += buttonWidth + buffer;
}
[_buttonsHorizontalScrollView
setContentSize:CGSizeMake(aButton.frame.size.width*_examsListArray.count+203, yCoord)];
}
- (void)whatever:(id)sender
{
UIButton *tempBtn = (UIButton *)sender;
[aButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[aButton setBackgroundColor:[UIColor colorWithRed:250.0f/255.0f green:255.0f/255.0f
blue:221.0f/255.0f alpha:1]];
aButton = tempBtn;
[aButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[aButton setBackgroundColor:[UIColor colorWithRed:133.0f/255.0f green:169.0f/255.0f
blue:83.0f/255.0f alpha:1]];
}
No I just want the first button to be selected as default when the view is loaded and whenever I select the other button it should change to deselected color.
if ([aButton tag] == 0)
{
[aButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[aButton setBackgroundColor:[UIColor colorWithRed:133.0f/255.0f green:169.0f/255.0f
blue:83.0f/255.0f alpha:1]];
}
I have used the above code to select as first button as default but first button is not getting changed when other button is clicked and it is changing only when the first button is clicked again due to this aButton = tempBtn.How to do that?
I suggest you to set the selected status title color of all your buttons and then add them to an array. Then when you press one of them just change their status, no need to handle colors or keep track of the currently selected button.
#property (strong, nonatomic) NSMutableArray *yourButtons;
- (void)viewDidLoad {
[super viewDidLoad];
_examsListDict1 = [[NSMutableDictionary alloc]init];
_examsListDict1[#"Exam Name"] = #"JEE Mains";
.........so on another 4 dictionaries
_examsListArray = [[NSMutableArray alloc] initWithObjects:_examsListDict1,_examsListDict2,_examsListDict3,_examsListDict4,_examsListDict5, nil];
NSUInteger i;
int xCoord=0;
int yCoord=0;
int buttonWidth=120;
int buttonHeight=50;
int buffer = 1;
// Generate background images
//
UIImage *backgroundImage = [self imageWithColor:[UIColor colorWithRed:250.0f/255.0f green:255.0f/255.0f blue:221.0f/255.0f alpha:1]];
UIImage *selectedBackgroundImage = [self imageWithColor:[UIColor colorWithRed:133.0f/255.0f green:169.0f/255.0f blue:83.0f/255.0f alpha:1]];
// Initialize your array
//
self.yourButtons = [[NSMutableArray alloc] init];
for (i = 0; i < [_examsListArray count]; i++) {
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
aButton.frame = CGRectMake(xCoord, yCoord,buttonWidth,buttonHeight );
[aButton setTitle:[[_examsListArray objectAtIndex:i] objectForKey:#"Exam Name"] forState:UIControlStateNormal];
[aButton setTag:i];
[aButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[aButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
[aButton setBackgroundImage:backgroundImage forState:UIControlStateNormal]
[aButton setBackgroundImage:selectedColorImage forState:UIControlStateSelected]
[aButton addTarget:self action:#selector(whatever:) forControlEvents:UIControlEventTouchUpInside];
[_buttonsHorizontalScrollView addSubview:aButton];
xCoord += buttonWidth + buffer;
// Select the first one
//
if (i == 0)
aButton.selected = YES;
// Add your newly created button to the array
//
[self.yourButtons addObject:aButton];
}
[_buttonsHorizontalScrollView setContentSize:CGSizeMake(aButton.frame.size.width*_examsListArray.count+203, yCoord)];
}
- (void)whatever:(id)sender {
for (UIButton *button in self.yourButtons) {
// Toggle selected flag
//
button.selected = button == sender;
}
}
+ (UIImage *)imageWithColor:(UIColor *)color {
// http://stackoverflow.com/a/24665476/1835155
//
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
TRUEThe unpressed button is UIControlStateNormal while the pressed button is UIControlStateSelected so set the colours you want based on the State. You typically do this in ViewDidload before anyone has a chance to use it - Below is a sample setup:
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setSelected:YES];
button.frame = CGRectMake(x, y, width, height);
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
[button setTitle:#"Button Title" forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundImage:[UIImage imageNamed:#"button.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"buttonActive.png"] forState:UIControlStateSelected];
[button setBackgroundImage:[UIImage imageNamed:#"buttonActive.png"] forState:UIControlStateHighlighted];
[button setBackgroundImage:[UIImage imageNamed:#"buttonActive.png"] forState:UIControlStateDisabled];
I believe what you should be doing when the trigger button is clicked is to just set the state to selected
- (IBAction)touchDown:(id)sender {
if (sender == trigger_button) {
[button setHighlighted:TRUE];
[button setSelected:TRUE];
}
In order to change the first button state when another button is clicked you have to "fake" a touch event. In the selector method of the other button do this:
For Example:
-(void)thirdButtonClicked:(id) sender
{
[firstButton sendActionsForControlEvents: UIControlEventTouchUpInside];
}

UIButton pressed, other one doesn't press

hi I have some code that creates two buttons and when a button is pressed an trigger is set and it changes the background and the text colour.
How could i make it so that once one is pressed the other cannot be pressed.
This creates the buttons
//learnmore button
UIButton *learnmorebutton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.view addSubview:learnmorebutton];
learnmorebutton.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];
learnmorebutton.frame = CGRectMake(0.0, 518.0, 159.0, 50.0);
[learnmorebutton setTitle:#"learn more" forState:UIControlStateNormal];
[learnmorebutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
learnmorebutton.titleLabel.font = [UIFont fontWithName:#"Helvetica Light" size:17.0];
//signup button
UIButton *signup = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.view addSubview:signup];
signup.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];
signup.frame = CGRectMake(162.0, 518.0, 160.0, 50.0);
[signup setTitle:#"sign up" forState:UIControlStateNormal];
signup.titleLabel.font = [UIFont fontWithName:#"Helvetica Light" size:17.0];
signup.titleLabel.textColor = [UIColor whiteColor];
These are the two triggers
[learnmorebutton addTarget:self action:#selector(learnMoreClickEvent:) forControlEvents:UIControlEventTouchUpInside];
[signup addTarget:self action:#selector(signupClickEvent:) forControlEvents:UIControlEventTouchUpInside];
These are the two events
- (void)learnMoreClickEvent:(UIButton *)sender
{
//sender is the button that was tapped
[sender setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
sender.backgroundColor = [UIColor whiteColor];
}
- (void)signupClickEvent:(UIButton *)sender
{
//sender is the button that was tapped
[sender setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
sender.backgroundColor = [UIColor whiteColor];
}
thanks all ...
You start by keep a reference to the button, preferably properties on the viewController that creates these buttons.
#interface MyViewController : UIViewController
#property (nonatomic, strong) UIButton *learnmorebutton;
#property (nonatomic, strong) UIButton *signup;
#end;
Then in the method either disable the button:
<button>.enabled = NO:
Or hide it:
<button>.hidden = YES:
like rckoenes mentioned you can declare those UIButton as properties and do that.. or
Assign Tags like this
//learnmore button
UIButton *learnmorebutton = [UIButton buttonWithType:UIButtonTypeCustom];
learnmorebutton.tag = 10;
learnmorebutton.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];
learnmorebutton.frame = CGRectMake(0.0, 518.0, 159.0, 50.0);
[learnmorebutton setTitle:#"learn more" forState:UIControlStateNormal];
[learnmorebutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
learnmorebutton.titleLabel.font = [UIFont fontWithName:#"Helvetica Light" size:17.0];
[learnmorebutton addTarget:self action:#selector(learnMoreClickEvent:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:learnmorebutton];
//signup button
UIButton *signup = [UIButton buttonWithType:UIButtonTypeRoundedRect];
signup.tag = 20;
signup.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];
signup.frame = CGRectMake(162.0, 518.0, 160.0, 50.0);
[signup setTitle:#"sign up" forState:UIControlStateNormal];
signup.titleLabel.font = [UIFont fontWithName:#"Helvetica Light" size:17.0];
signup.titleLabel.textColor = [UIColor whiteColor];
[self.view addSubview:signup];
[signup addTarget:self action:#selector(signupClickEvent:) forControlEvents:UIControlEventTouchUpInside];
By Using tags to access the buttons
- (void)learnMoreClickEvent:(UIButton *)sender
{
UIButton *signUp = (UIButton*)[self.view viewWithTag:20]
signUp.enabled = NO;
//sender is the button that was tapped
[sender setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
sender.backgroundColor = [UIColor whiteColor];
}
- (void)signupClickEvent:(UIButton *)sender
{
UIButton *learnmorebutton = (UIButton*)[self.view viewWithTag:10]
learnmorebutton.enabled = NO;
//sender is the button that was tapped
[sender setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
sender.backgroundColor = [UIColor whiteColor];
}
Hope this gives you an Idea.
Basically if your looking for toggling, you can always use UISegementControl. I mean it depends on your requirements.
Set the enabled property of the button you want to disable to NO: <button>.enabled = NO;

Trying to place UILabel on top of UIButton in a custom UIAlertView

What I'm trying to do is place a UILabel on top of a UIButton.
The UIButton have an image as a background and the result is I cannot see the normal text on the UIButton, therefore I think to place the UILabel on top of it.
My code is:
NAlertView *customAlert = [[NAlertView alloc] initWithTitle:#"title" message:#"message" delegate:self cancelButtonTitle:#"ok" otherButtonTitles:#"cancel", nil];
[customAlert show];
NAlertView.m:
- (void)layoutSubviews
{
for (id obj in self.subviews) //Search in all sub views
{
if ([obj isKindOfClass:[UIImageView class]]) // Override UIImageView (Background)
{
UIImageView *imageView = obj;
[imageView setImage:[UIImage imageNamed:#"CustomAlertView"]];
[imageView setAlpha:0.7];
[imageView sizeToFit];
}
if ([obj isKindOfClass:[UILabel class]]) // Override UILabel (Title, Text)
{
UILabel *label = (UILabel*)obj;
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
label.shadowColor = [UIColor clearColor];
}
if ([obj isKindOfClass:[UIButton class]]) // Override the UIButton
{
UIButton *button = (UIButton*)obj;
CGRect buttonFrame = button.frame; // Change UIButton size
buttonFrame.size = CGSizeMake(127, 35);
CGFloat newYPos = buttonFrame.origin.y + 9; // Change UIButton position
buttonFrame.origin.y = newYPos;
button.frame = buttonFrame;
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
switch (button.tag)
{
case kFIRST_BUTTON:
{
[button setImage:[UIImage imageNamed:#"short_button_gold_off.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"short_button_gold_on.png"] forState:UIControlStateHighlighted];
}
break;
case kSECOND_BUTTON:
{
[button setImage:[UIImage imageNamed:#"short_button_black_off.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"short_button_black_on.png"] forState:UIControlStateHighlighted];
}
break;
}
}
}
[self updateConstraints];
}
Result:
That means you just want to add Title on UIButton?
Try This -
[button setBackgroundImage:[UIImage imageNamed:#"short_button_gold_off.png"] forState:UIControlStateNormal];
[button setTitle:#"Title" forState:UIControlStateNormal];
Replace
[button setImage:[UIImage imageNamed:#"short_button_gold_off.png"] forState:UIControlStateNormal];
to
[button setBackgroundImage:[UIImage imageNamed:#"short_button_gold_off.png"] forState:UIControlStateNormal];
change all such possibilities.
and use the below method to change the text of the button
[button setTitle:#"text" forState:UIControlStateNormal];
Hope it helps.

Resources