how to create a button in uiview programmatically in iphone - ios

In my iPhone application, how to add next and previous buttons in uiview programmatically?

You can create dynamic buttons using the following code
UIButton *but=[UIButton buttonWithType:UIButtonTypeRoundedRect];
but.frame= CGRectMake(200, 15, 15, 15);
[but setTitle:#"Ok" forState:UIControlStateNormal];
[but addTarget:self action:#selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:but];

Related

UIButton programatically is not working in iOS

When I click on the UIButton nothing happened
checkbox = [[UIButton alloc] initWithFrame:CGRectMake(73, 324, 15, 15)];
[checkbox setBackgroundImage:[UIImage imageNamed:#"checkbox.png"] forState:UIControlStateNormal];
[checkbox setBackgroundImage:[UIImage imageNamed:#"checkbox-checked.png"] forState:UIControlStateSelected];
[checkbox addTarget:self action:#selector(autologin) forControlEvents:UIControlEventTouchUpInside];
[self.background addSubview:checkbox];
You have to create button like below.
UIButton *checkbox = [UIButton buttonWithType:UIButtonTypeCustom]; // this is important
checkbox.frame = CGRectMake(73, 324, 15, 15)];
[checkbox setBackgroundImage:[UIImage imageNamed:#"checkbox.png"] forState:UIControlStateNormal];
[checkbox setBackgroundImage:[UIImage imageNamed:#"checkbox-checked.png"] forState:UIControlStateSelected];
[checkbox addTarget:self action:#selector(autologin) forControlEvents:UIControlEventTouchUpInside];
[self.background addSubview:checkbox];
You have to create the button first using button property and then give its frame.
If still its not working then self.background addSubview should be self.view addSubview.
What is self.background?
Your code is perfectly fine. You need to check the following .
check whether self.background is placed properly in your view heirarchy, I tried your code with self.view instead of self.background like below and it works perfectly.
self.checkbox = [[UIButton alloc] initWithFrame:CGRectMake(73, 324, 50, 50)];
[self.checkbox setBackgroundImage:[UIImage imageNamed:#"checkbox1.png"] forState:UIControlStateNormal];
[self.checkbox setBackgroundImage:[UIImage imageNamed:#"checkbox2.png"] forState:UIControlStateSelected];
[self.checkbox addTarget:self action:#selector(autologin) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.checkbox];
From the error message that you have given
"
Warning: Attempt to present <enter_the_den: 0x7fbf1bd4c4e0> on <ViewController: 0x7fbf1bc771c0> whose view is not in the window hierarchy!
I suspect the issue is in your code inside the method autologin, just comment all the code in your method and just try a log , it should work.
refer this : whose view is not in the window hierarchy
If you read the Apple Documentation for UIButton and look under the heading marked as Creating Button you'll find that the way to create a UIButton is to use buttonWithType: and not initWithFrame:.
Return Value
A newly created button.
Discussion
This method is a convenience constructor for creating button objects with specific configurations. If you subclass UIButton, this method does not return an instance of your subclass. If you want to create an instance of a specific subclass, you must alloc/init the button directly.
When creating a custom button—that is a button with the type UIButtonTypeCustom—the frame of the button is set to (0, 0, 0, 0) initially. Before adding the button to your interface, you should update the frame to a more appropriate value.
The only time when you should use alloc/init with a UIButton is when you have subclassed UIButton however your init method should then implement the buttonWithType: and pass in UIButtonTypeCustom.
So change:
checkbox = [[UIButton alloc] initWithFrame:CGRectMake(73, 324, 15, 15)];
To
// Create a button of type custom
checkbox = [UIButton buttoWithType:UIButtonTypeCustom];
// Now set the frame for your button
[checkbox setFrame:CGRectMake(73, 324, 15, 15)];
// Continue with the rest of your button code
[checkbox setBackgroundImage:[UIImage imageNamed:#"checkbox.png"] forState:UIControlStateNormal];
[checkbox setBackgroundImage:[UIImage imageNamed:#"checkbox-checked.png"] forState:UIControlStateSelected];
[checkbox addTarget:self action:#selector(autologin) forControlEvents:UIControlEventTouchUpInside];
[self.background addSubview:checkbox];
Check userInteractionEnabled property of your background property.
But i see why you have no feedback on button if rest is fine - you use UIControlStateSelected for setting image when button has touch, but you must use UIControlStateHighlighted

I cannot make a UIButton programatically

I have searched all of the internet, it seems that every post on this topic leaves out one vital piece of info that i need. no matter what people tell me to do, i cannot get rid of a "undeclared identifier" error with everything/anything regarding my method for my button's action. I have picked at the code so much that it is totally jacked up. Can anyone tell me what exactly i need to to do make a proper interactive button? Everything that goes in the .m file and .h file for viewcontroller? please don't assume that I already have a certain vital piece of code, because I might not. I am new to objective-c.
Thank you!!!!!!!! ( it is for ipad by the way, and i am avoiding using the storyboard )thanks!! i am very frustrated at this point.
here is my code so far
UIButton *add = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[add addTarget:self action:#selector(aMethod:) forControlEvents:UIControlEventTouchDown];
[add setTitle:#"add new" forState:UIControlStateNormal];
add.frame = CGRectMake(100, 100, 100, 100);
[objects addSubview:add];
- (void)aMethod:(UIButton*)button
{
NSLog(#"Button clicked.");
}
And the problem isn't that it runs and then crashes, the problem is that i can't run it to begin with as long as I have this error "Use of undeclared identifier 'aMethod'"
.what is object in this line [object addSubview:add];
try this
UIButton *add = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[add addTarget:self action:#selector(aMethod) forControlEvents:UIControlEventTouchDown];
[add setTitle:#"add new" forState:UIControlStateNormal];
add.frame = CGRectMake(100, 100, 100, 100);
[object addSubview:add];
- (void)aMethod
{
NSLog(#"Button clicked.");
}
Please try this code
UIButton *submitButton=[[UIButton alloc]initWithFrame:CGRectMake(120, 300, 70, 50)];
[submitButton.layer setCornerRadius:10.0];
[submitButton setTitle:#"Submit" forState:UIControlStateNormal];
[submitButton addTarget:self action:#selector(submitAction:)forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:submitButton];
try this.
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];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];
Once you press the button it will look for buttonClicked: action event automatically
-(void) buttonClicked:(UIButton *)clicked{ //Your action goes here...}

Lay a detail disclosure button on a UIButton

How do I add a detail disclosure button to the right side of my text in a UIButton, perhaps using edge insets?
You can create a UIButton of Type UIButtonTypeDetailDisclosure and add it as a subview of your button.
Like:
UIButton *mainButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[mainButton setTitle:#"Click" forState:UIControlStateNormal];
[mainButton setFrame:CGRectMake(10, 10, 100, 50)];
[self.view addSubview:mainButton];
//Detail Disclosure button
UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[detailButton setFrame:CGRectMake(70, 10, 30, 30)];
[mainButton addSubview:detailButton];

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

UIButton inside UIImageView does not respond to taps

I have a scroll view, which has an image view with an image as a subview, and the image view has a UIButton as one of its subviews. The problem is, I am not able to click on the button. I can SEE the button, but I cannot tap on it.
Can someone tell me what is it that I am messing up? Any help is greatly appreciated! Thanks!
Below is the code:
scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"img.jpg"]];
scrollView.delegate = self;
self.view = scrollView;
// add invisible buttons
[self addInvisibleButtons];
[scrollView addSubview:imageView];
addInvisibleButtons has the following code:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(buttonHandler) forControlEvents:UIControlEventAllEvents];
[button setTitle:#"point" forState:UIControlStateNormal];
button.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
[self.imageView addSubview:button];
UIImageView has userInteractionEnabled set to NO/ false by default.
You are adding the button as a subview to the image view. You should set it to YES / true.
May I ask why are you adding invisible UIButtons to UIImageView?
Seems like a bad practice, notice Interface Builder doesn't allow you to add UIButton inside them.
If you want an image with touch handling, you can:
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:#selector(buttonHandler) forControlEvents:UIControlEventAllEvents];
[button setTitle:#"point" forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"img.jpg"] forState:UIControlStateNormal];
[button setFrame:CGRectMake(0.0, 0.0, 40.0, 40.0)];
[scrollView addSubview:button];
You can have addInvisibleButtons implementation as
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundColor:[UIColor clearColor]];
[button addTarget:self action:#selector(buttonHandler) forControlEvents:UIControlEventAllEvents];
[button setTitle:#"point" forState:UIControlStateNormal];
button.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
self.imageView.userInteractionEnabled = YES;
[self.imageView addSubview:button];
If you want to have UIButton as completely invisible then you need to remove a line
[button setTitle:#"point" forState:UIControlStateNormal];
since it use to show text on UIButton which make it visible.
This may resolve your issue.

Resources