Button Title changing unexpectedly in UIButton - ios

I have a UIButton created with the following code:
applyButton= [[UIButton alloc] initWithFrame:CGRectMake(75, 636, 150, 50)];
[applyButton titleColorForState:UIControlStateNormal];
[applyButton setTitle:#"MyTitle" forState:UIControlStateNormal];
applyButton.titleLabel.textColor=[UIColor lightGrayColor];
[[applyButton layer] setBorderWidth:2.0f];
[[applyButton layer] setBorderColor:[[UIColor lightGrayColor] CGColor]];
applyButton.layer.cornerRadius = 10;
[applyButton addTarget:self action:#selector(saveSettingsToNSUser:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:applyButton];
The button appears with "MyTitle" in light gray colour initially.
But when i presses it the title colour turns white and the "saveSettingsToNSUser" function is called.There is NOTHING inside the function "saveSettingsToNSUser" which affects the button. I don't understand what is happening. Am i missing something?

You should use setTitleColor:forState: to set title label color.
[applyButton setTitleColor: [UIColor lightGrayColor] forState: UIControlStateNormal];

applyButton= [[UIButton alloc] initWithFrame:CGRectMake(75, 636, 150, 50)];
//set title
[applyButton setTitle:#"MyTitle" forState:UIControlStateHighlighted];
[applyButton titleColorForState:UIControlStateNormal];
[applyButton setTitle:#"MyTitle" forState:UIControlStateNormal];
applyButton.titleLabel.textColor=[UIColor lightGrayColor];
[[applyButton layer] setBorderWidth:2.0f];
[[applyButton layer] setBorderColor:[[UIColor lightGrayColor] CGColor]];
applyButton.layer.cornerRadius = 10;
[applyButton addTarget:self action:#selector(saveSettingsToNSUser:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:applyButton];

Related

UIButton shadow layer effect

I am creating a UIButton similar to this image:
I tried it by using following code:
+(void)createShadowOnView:(UIView *)view color:(UIColor *)color width:(CGFloat)width height:(CGFloat)height shadowOpacity:(CGFloat)shadowOpacity andShadowRadius:(CGFloat)radius{
view.layer.masksToBounds = NO;
view.layer.shadowColor = color.CGColor;
view.layer.shadowOffset = CGSizeMake(width,height);
view.layer.shadowOpacity = shadowOpacity;
[view.layer setShadowRadius:radius];
}
I was able to achieve this:
I want shadow effect on Button to be kept only on bottom part.
How can I achieve desired effect.
Maybe you should set view's backgroundcolor, so the title has no shadow, you can set view.layer.shadowOffset to change the shadow size.
UIButton *customBTn = [UIButton buttonWithType:UIButtonTypeCustom];
customBTn.backgroundColor = [UIColor whiteColor];
customBTn.frame = CGRectMake(100, 100, 200, 50);
[customBTn setTitle:#"Sign Up" forState:UIControlStateNormal];
[customBTn setTitleColor:[UIColor colorWithRed:1/255.0 green:168/255.0 blue:244/255.0 alpha:1.0] forState:UIControlStateNormal];
customBTn.layer.borderColor = [UIColor colorWithRed:1/255.0 green:168/255.0 blue:244/255.0 alpha:1.0].CGColor;
customBTn.layer.borderWidth = 2;
customBTn.layer.cornerRadius = 25;
customBTn.layer.shadowColor = [UIColor lightGrayColor].CGColor;
customBTn.layer.shadowOffset = CGSizeMake(0,8);
customBTn.layer.shadowOpacity = 0.9;
[self.view addSubview:customBTn];
Output :-
Only issue with your code is you missed to set background colour for your sign up button. Use this code,
self.signUpButton.backgroundColor = [UIColor whiteColor];
[UIView createShadowOnView:self.signUpButton color:[UIColor lightGrayColor] width:0 height:2 shadowOpacity:0.3 andShadowRadius:0];
Preview:
Code:
UIButton *buttonWithShadow = [UIButton buttonWithType:UIButtonTypeCustom];
[buttonWithShadow setFrame:CGRectMake(10.0f, 10.0f, 300.0f, 50.0f)];
[buttonWithShadow setTitle:#"SIGN UP" forState:UIControlStateNormal];
[buttonWithShadow setTitle:#"SIGN UP" forState:UIControlStateHighlighted];
[buttonWithShadow.titleLabel setFont:[UIFont boldSystemFontOfSize:15.0f]];
[buttonWithShadow setTitleColor:[UIColor colorWithRed:1.0f/255.0 green:168.0f/255.0 blue:244.0f/255.0 alpha:1.0f] forState:UIControlStateNormal];
[buttonWithShadow setBackgroundColor:[UIColor whiteColor]];
[buttonWithShadow.layer setBorderColor:[UIColor colorWithRed:1.0f/255.0 green:168.0f/255.0 blue:244.0f/255.0 alpha:1.0f].CGColor];
[buttonWithShadow.layer setBorderWidth:2.0f];
[buttonWithShadow.layer setCornerRadius:(buttonWithShadow.frame.size.height / 2.0f)];
[buttonWithShadow.layer setShadowColor:[UIColor lightGrayColor].CGColor];
[buttonWithShadow.layer setShadowOpacity:0.3f];
[buttonWithShadow.layer setShadowRadius:0.0f];
[buttonWithShadow.layer setShadowOffset:CGSizeMake(0.0f,2.0f)];
[buttonWithShadow.layer setMasksToBounds:NO];
[self.view addSubview:buttonWithShadow];

UIButton used to work until I added UIlabel

I tried to label a UIbutton which used to work until I added a UIlabel layer.
At first the button looked like this and later I filled the space either side of the label thus
When I click either button the following error report is logged:
unexpected nil window in _UIApplicationHandleEventFromQueueEvent, _windowServerHitTestWindow: <UIWindow: 0x7fe460e338d0; frame = (0 0; 320 480); opaque = NO; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x7fe460e326a0>; layer = <UIWindowLayer: 0x7fe460e33d80>>
The button works if I click above the 4 letters or the ^ symbols.
Adding the label layer seems to effectively mask the UIButton. The hot spot (i.e. where the button works) seems to be above the letters or the ^ symbols. Can anyone explain why this might be happening ?
GlobalView.h file imports Quartz.Core/QuartzCore.h and UIKit/UIKit.h
Here’s the code in GlobalView.m
[UIButton buttonWithType:UIButtonTypeSystem];
UIButton *helpButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
helpButton.frame = CGRectMake(72.0f, 472.0f, 188.0f, 32.0f);
helpButton.layer.cornerRadius = 10;
[helpButton.layer setBorderColor:[[UIColor whiteColor] CGColor]];
[[helpButton layer] setBorderWidth:0.5f];
[helpButton addTarget:self action:#selector(goToHelp) forControlEvents:UIControlEventTouchUpInside];
UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(72.0f, 472.0f, 188.0f, 32.0f);
label.text=#“help”;
[label setTextAlignment: NSTextAlignmentCenter];
label.textColor=[UIColor whiteColor];
[label.layer setBorderColor:[[UIColor whiteColor] CGColor]];
[helpButton addSubview:label];
[self.view addSubview:helpButton];
Is the error log hinting that there is something I can do to recognise a tap gesture ? or is it better to try and get the UIButton to respond ?
CHANGE #1
The same problem occurs when I use setTitle to label the button
[UIButton buttonWithType:UIButtonTypeSystem];
UIButton *helpButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
helpButton.frame = CGRectMake(72.0f, 472.0f, 188.0f, 32.0f);
[helpButton setTitle: #"Transposed Dekany" forState: UIControlStateNormal];
helpButton.layer.cornerRadius = 10;
[helpButton.layer setBorderColor:[[UIColor whiteColor] CGColor]];
[[helpButton layer] setBorderWidth:0.5f];
[helpButton addTarget:self action:#selector(goToHelp) forControlEvents:UIControlEventTouchUpInside];
UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0,0,188,32)];
[label.layer setBorderColor:[[UIColor whiteColor] CGColor]];
[helpButton addSubview:label];
[self.view addSubview:helpButton];
CHANGE #2
UIButton *helpButton = [UIButton buttonWithType:UIButtonTypeCustom];
[helpButton setTitleColor:[UIColor whiteColor] forState: UIControlStateNormal];
[helpButton setTitle: #"^^^^^^^Help^^^^^^^^^" forState: UIControlStateNormal];
helpButton.frame = CGRectMake(72.0f, 472.0f, 188.0f, 32.0f); //same width and height
helpButton.layer.cornerRadius = 10;
[helpButton.layer setBorderColor:[[UIColor whiteColor] CGColor]];
[[helpButton layer] setBorderWidth:0.5f];
helpButton.clipsToBounds = YES;
[helpButton addTarget:self action:#selector(goToHelp) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:helpButton];

UIButton is not appear on view in iOS 9

I have created my project in iOS 8 it works fine and now i am trying to run in iOS9.UIButton is not appearing. I have created a button programmatically, but it is not appearing only in iOS9. It works fine in iOS7 and iOS8. Please see the below code.
UIButton *NewOrderButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[NewOrderButton addTarget:self action:#selector(ButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[NewOrderButton setTitle:#"Create" forState:UIControlStateNormal];
NewOrderButton.frame = CGRectMake((self.view.frame.size.width-200)/2,(self.view.frame.size.height+80)/2, 200, 40.0);
[NewOrderButton.layer setBorderWidth:1];
[NewOrderButton.layer setBorderColor:[[UIColor whiteColor] CGColor]];
NewOrderButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[self.view addSubview:NewOrderButton];
If i debug this code, then the button is appearing.
I ran this code in my environment. just change following
[self.view addSubview:_NewOrderButton];
to
[self.view addSubview:NewOrderButton];
i ran it in iOS 9 it works fine..
and change the last line of your code with these..
[self.view addSubview:NewOrderButton];
i hope it helps..
You should add your code to
-(void)viewDidLayoutSubviews {
UIButton *NewOrderButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[NewOrderButton addTarget:self action:#selector(ButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[NewOrderButton setTitle:#"Create" forState:UIControlStateNormal];
NewOrderButton.frame = CGRectMake((self.view.frame.size.width-200)/2,(self.view.frame.size.height+80)/2, 200, 40.0);
[NewOrderButton.layer setBorderWidth:1];
[NewOrderButton.layer setBorderColor:[[UIColor whiteColor] CGColor]];
NewOrderButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[self.view addSubview:NewOrderButton];
}
Elavarasan I applied your coding in viedDidLoad and viewDidLayoutSubviews,it works fine.Also please set the background color for the button.Below is the code.Again apply this code in your class.Definitely you will get solution.
UIButton *NewOrderButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[NewOrderButton addTarget:self action:#selector(ButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[NewOrderButton setTitle:#"Create" forState:UIControlStateNormal];
NewOrderButton.backgroundColor = [UIColor blueColor];
NewOrderButton.frame = CGRectMake((self.view.frame.size.width/250)/2,(self.view.frame.size.height+80)/2, 200, 40.0);
[NewOrderButton.layer setBorderWidth:1];
[NewOrderButton.layer setBorderColor:[[UIColor greenColor] CGColor]];
NewOrderButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[self.view addSubview:NewOrderButton];

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];

titleLabel.textColor for Uibutton

In the tableView footer I have a button
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
UIView *mainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 39)];
[mainView setBackgroundColor:[UIColor clearColor]];
UIButton *themebutton= [UIButton buttonWithType:UIButtonTypeCustom];
themebutton.backgroundColor= [UIColor redColor];
[themebutton.layer setCornerRadius:5.8f];
themebutton.frame= CGRectMake(15, 0, 172, 39);
[themebutton setTitle:#"Create New" forState:UIControlStateNormal];
[themebutton setTitle:#"Create New" forState:UIControlStateSelected];
[themebutton setTitle:#"Create New" forState:UIControlStateHighlighted];
themebutton.titleLabel.font=[UIFont fontWithName:#"Raleway" size:19.8];
themebutton.titleLabel.textColor=UIColorFromRGB(0x707070);
[mainView addSubview:themebutton];
return mainView;
}
I have put the titleLabel.textColor as grey but when I click on that button it always convert the text color to white . Why this happening can anyone help me .
You need to set the color depending on the state, for example:
[themebutton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[themebutton setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted];
This will display the label red, and when pressed it will turn green.

Resources