UIButton is not appear on view in iOS 9 - ios

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

Related

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

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

Button Title changing unexpectedly in UIButton

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

xcode invisible button with text

I know I could do this:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(x, y, width, height);
[button setTitle:text forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];
but that leaves a border around the button.
I could also do this:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(x, y, width, height);
[button setTitle:text forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
then put a UILabel behind the button.
Is there a way to accomplish this ^ without needing two objects?
UPDATE:
To clarify my question:
I want the have text, only text, that when pressed performs a method call. For reasons that are complicated to explain I need to do this by way of a button. So, how do I make the text of a button the only visible part of the button. No border. No background. Just text.
UIButton *btnLikeUserCount = [[[UIButton alloc] init] autorelease];
[btnLikeUserCount.titleLabel setFont:[UIFont boldSystemFontOfSize:12]];
btnLikeUserCount.frame = CGRectMake(posx,posy,width,height);
[btnLikeUserCount setTitle:text forState:UIControlStateNormal];
[btnLikeUserCount setTitleColor:[UIColor colorWithRed:50/255.0 green:79/255.0 blue:133/255.0 alpha:1.0] forState:UIControlStateNormal];
[btnLikeUserCount addTarget:self action:#selector(btnLikeUserCountClick:) forControlEvents:UIControlEventTouchUpInside];
no need to take label object.
I can't get what you want exactly but you might be missed [self.view addSubview:button] in your code. so you can not display button with text.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(x, y, width, height);
[button setTitle:text forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
Your second code is working fine. In the below image hello is a button.
My code is:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(110, 100, 100, 50);
[button setTitle:#"Hello" forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
in this way u can do this
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100, 400, 100, 50);
[button setTitle:#"Button text" forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[button.layer setBorderColor: [UIColor clearColor]];
[button.layer setBorderWidth: 0.0];
[self.view addSubview:button];

Custom UIButton in UIscrollview

I am using xcode 4.6 to develop an app. Here i want to add UIButton programmatically to UIscrollview. This is the code i follow.
UIButton *bt =[[UIButton alloc]initWithFrame:frame];
bt=[UIButton buttonWithType:UIButtonTypeCustom];
[bt setTitle:#"Custom Button" forState:UIControlStateNormal];
[bt addTarget:self action:#selector(userTappedOnLink:) forControlEvents:UIControlEventTouchUpInside];
bt.backgroundColor = [UIColor grayColor];
bt.titleLabel.textColor=[UIColor blueColor];
[self.mainscrollview addSubview:bt];
[self.mainscrollview bringSubviewToFront:bt];
Now the problem is that Button gets disappeared (technically its textcolor becomes white) on click. I checked keeping UIscrollview color to red that th button was still in the view but i cant get the reason why its text color changed and how do i undo dis.
Basically I wan to create a clickable link using UIbutton.
I know uitextview approach (datadetectortype) but its of no use as i want to show different text in the label for the link and the actual link.
Note: The textcolor doesnt change back to blue and remains white only.
Thanks in advance.
Try the below code
UIButton *bt =[UIButton buttonWithType:UIButtonTypeCustom];
bt.frame = CGRectMake(50.0, 50.0, 100.0, 50.0);
[bt setTitle:#"Custom Button" forState:UIControlStateNormal];
[bt addTarget:self action:#selector(userTappedOnLink:) forControlEvents:UIControlEventTouchUpInside];
bt.backgroundColor = [UIColor grayColor];
bt.titleLabel.textColor=[UIColor blueColor];
[self.scrollTest addSubview:bt];
-(void)userTappedOnLink:(UIButton*)sender
{
NSLog(#"Test ..");
[self performSelector:#selector(changeBtnTextColor:) withObject:sender afterDelay:1.0];
}
-(void)changeBtnTextColor:(UIButton*)btn
{
btn.titleLabel.textColor=[UIColor blueColor];
}
hope it will work for you.
use below code you will get your solution
UIButton *bt =[[UIButton alloc]initWithFrame:frame];
bt=[UIButton buttonWithType:UIButtonTypeCustom];
[bt setTitle:#"Custom Button" forState:UIControlStateNormal];
[bt addTarget:self action:#selector(userTappedOnLink:) forControlEvents:UIControlEventTouchUpInside];
[bt setBackgroundColor:[UIColor grayColor]];
[bt setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[self.mainscrollview addSubview:bt];
[self.mainscrollview bringSubviewToFront:bt];
Use the below code:
[bt setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
Problem is you are set Title to titleLabel and you are changing textColor of buttonTitle color.
All the best !!!
check Button's Fram .. is it valid or not ??
and remove bt=[UIButton buttonWithType:UIButtonTypeCustom]; line from your code.

Resources