How do I create a simple button without using the Interface builder? - iphone

How do I create a simple button without using the Interface builder?

You would need to declare a UIButton like this:
UIButton *newButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 40)];
[newButton setTitle:#"Some Button" forState:UIControlStateNormal];
You would then add the button to a view -
[self.view addSubview: newButton]; // (could be another view other then self.view)
And finally, if you want some action to occur when pressing that button, you would add:
[newButton addTarget:self action:#selector(doSomething)
forControlEvents:UIControlEventTouchUpInside];
and of course declare that function:
- (void) doSomething
{
NSLog(#"Button pressed");
}
And of course don't forget to release the button.

Try with below
UIButton *playButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect];
playButton.frame = CGRectMake(110.0, 360.0, 100.0, 30.0);
[playButton setTitle:#"Play" forState:UIControlStateNormal];
playButton.backgroundColor = [UIColor clearColor];
[playButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal ];
UIImage *buttonImageNormal = [UIImage imageNamed:#"blueButton.png"];
UIImage *strechableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[playButton setBackgroundImage:strechableButtonImageNormal forState:UIControlStateNormal];
UIImage *buttonImagePressed = [UIImage imageNamed:#"whiteButton.png"];
UIImage *strechableButtonImagePressed = [buttonImagePressed stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[playButton setBackgroundImage:strechableButtonImagePressed forState:UIControlStateHighlighted];
[playButton addTarget:self action:#selector(playAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:playButton];

like this-
custom button -
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 30.0f)];
system buttom -
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

Related

UIBarButtonItem init with custom view selector not working properly

I'm trying for hours to make a left button work properly and mimic a back button.
My code to create the button:
UIBarButtonItem *backButton = [self customBarButton:#"back_button" imageHiglighted:#"settings_button_highlighted" x:20 y:0 widthDivider:2.6 heightDivider:2.6];
backButton.target = self;
backButton.action = #selector(buttonPressed:);
self.navigationItem.leftBarButtonItem = backButton;
Here the method called to create custom button:
- (UIBarButtonItem *)customBarButton:(NSString *)imageDefault imageHiglighted:(NSString *)imageHighlighted x:(float)x y:(float)y widthDivider:(float)widthDivider heightDivider:(float)heightDivider {
UIImage *customImageDefault = [UIImage imageNamed:imageDefault];
UIImage *customImageHighlighted = [UIImage imageNamed:imageHighlighted];
CGRect frameCustomButton = CGRectMake(x, y, customImageDefault.size.width/widthDivider, customImageDefault.size.height/heightDivider);
UIButton *customButton = [[UIButton alloc] initWithFrame:frameCustomButton];
[customButton setBackgroundImage:customImageDefault forState:UIControlStateNormal];
[customButton setBackgroundImage:customImageHighlighted forState:UIControlStateHighlighted];
UIBarButtonItem *barCustomButton =[[UIBarButtonItem alloc] initWithCustomView:customButton];
return barCustomButton;
}
And the action:
-(void)buttonPressed:(id) sender{
NSLog(#"Entered");
SearchViewController *ViewController = [[SearchViewController alloc] init];
[self.navigationController pushViewController:ViewController animated:YES];
}
So I was able to make it with a simple UIButton but not with a UIButtonBarItem and I really don't know what's going on with it.
If you could help me I'd be very grateful.
Thanks.
Do this add selector to custom button as it is view of bar buttom:
[customButton addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
EDIT : Note : the target and action of the UIBarButtonItem apply to custom views.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"goback.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(//HERE ! i don't know put what thing !) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 32, 32)];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
TRY THIS
self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:[GlobalMethods buttonWithImage:#"top-nav-back" heighlightImageName:nil buttonFrame:CGRectMake(102, 1, 50, 30) selectorName:#selector(btnBackClicked:) target:self]];
+ (UIButton*)buttonWithImage:(NSString*)normalImageName heighlightImageName:(NSString*)heighlightImageName buttonFrame:(CGRect)buttonFrame selectorName:(SEL)selectorName target:(id)target
{
UIButton *objButton = [UIButton buttonWithType:UIButtonTypeCustom];
[objButton setFrame:buttonFrame];
[objButton addTarget:target action:selectorName forControlEvents:UIControlEventTouchUpInside];
[objButton setImage:[GlobalMethods getImageFromResourceBundle:normalImageName] forState:UIControlStateNormal];
if(heighlightImageName)
[objButton setImage:[GlobalMethods getImageFromResourceBundle:heighlightImageName] forState:UIControlStateDisabled];
if(heighlightImageName)
[objButton setImage:[GlobalMethods getImageFromResourceBundle:heighlightImageName] forState:UIControlStateSelected];
return objButton;
}
Try this..
[customButton addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
in your customBarButton method..
I mean to say give selector to your button instead of bar button item..
Check and reply..
Just put this line in your code..
[backButton addTarget:self action:#selector(backBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
This worked for me
UIButton * tmpButton = [self generateButtonwithImageName:#"Back.png" andSize:CGRectMake(0, 0, 55, 30)];
UIBarButtonItem * backButton = [[UIBarButtonItem alloc] initWithCustomView:tmpButton];
[tmpButton addTarget:self action:#selector(reload) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem=backButton;
-(UIButton*)generateButtonwithImageName :(NSString*)imageName andSize:(CGRect)rect{
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame=rect;
[button setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
return button;
}
//working for me
if([version floatValue] < 7.0)
{
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"navi-bar.png"] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.opaque = YES;
closeButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 35, 30)];
[closeButton setImage:[UIImage imageNamed:#"btn_end.png"] forState:UIControlStateNormal];
[closeButton setImage:[UIImage imageNamed:#"btn_end_on.png"] forState:UIControlStateHighlighted];
[closeButton addTarget:self action:#selector(actionClose) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:closeButton];
}
else{
/*add button back to navigation*//*add buttom back to navigation*/
UIBarButtonItem *itemBack = [[UIBarButtonItem alloc]initWithTitle:#"完了" style:UIBarButtonItemStylePlain target:self action:#selector(actionClose)];
self.navigationItem.rightBarButtonItem = itemBack;
}

iphone customizing UIButtons

how to customize the UIButton so that it will display other than the default button style.
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(10.0, 20.0, 50.0, 50.0); // You can change the x, y, width, height.
[btn setImage:[UIImage imageNamed:#"img.png"] forState:UIControlStateNormal];
[self.view addSubView:btn];
You can do something like this:
UIImage *btnImage = [UIImage imageNamed:#"images-01.png"];
UIButton *btnGo = [UIButton buttonWithType:UIButtonTypeCustom];
btnGo.frame = CGRectMake(85,123, 100, 26);
btnGo.backgroundcolor = [UIColor clearColor];
btnGo. setImage:btnImage forState: UIcontrolStateNormal];
[btnGo.titleLabel setFont: [UIFont systemFontOfSize:14]];
btnGo addTarget: self action:#selector(btnGoClicked) forControlEvents: UIControlEventTouchUpInside];
You can use this
UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom];
customButton.frame = CGRectMake(899, 5, 82, 55);
customButton.tag = 123;
[customButton addTarget:self action:#selector(buttonToggleForHide) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:customButton];
and make event for that using
-(void)buttonToggleForHide{...}
In interface build simply change the type from rounded rect to custom and then specify a new background image.
In code:
UIButton* b = [UIButton buttonWithType:UIButtonTypeCustom];
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
Set the background image on it, set Type=Custom in interface builder.
Do you mean display button with custom image? You can try this:
UIButton *button = [[UIButton alloc]initWithImage:[UIImage imageNamed:#"button.png"]];
[button release];
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
button.frame = urRequiredFrame;
[button setImage:yourCustomImage forState:UIControlStateNormal];
button addTarget:self action:#selector(buttonPressed:) forControlEvents: UIControlEventTouchUpInside];
[self.view addSubView:button];
#shabbir 1st select the button then press cmd+1 change the button type to custom
if it right select my answer
other way is that
UIButton* b = [UIButton buttonWithType:UIButtonTypeCustom];
And if you want to call image on button then use this
UIButton *button = [[UIButton alloc]initWithImage:[UIImage imageNamed:#"button.png"]];
[button release];

Request for member view in something not in a structure or union?

UIButton *playButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect];
playButton.frame = CGRectMake(110.0, 360.0, 100.0, 30.0);
[playButton setTitle:#"Play" forState:UIControlStateNormal];
playButton.backgroundColor = [UIColor clearColor];
[playButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal ];
UIImage *buttonImageNormal = [UIImage imageNamed:#"blueButton.png"];
UIImage *strechableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[playButton setBackgroundImage:strechableButtonImageNormal forState:UIControlStateNormal];
UIImage *buttonImagePressed = [UIImage imageNamed:#"whiteButton.png"];
UIImage *strechableButtonImagePressed = [buttonImagePressed stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[playButton setBackgroundImage:strechableButtonImagePressed forState:UIControlStateHighlighted];
[playButton addTarget:self action:#selector(playAction:) forControlEvents:UIControlEventTouchUpInside];
**//[self.view addSubview:playButton];**
If you are writing this in a class which is a subclass of UIView
you should try
[self addSubview:playButton];
First create the view
UIView *myview=[[UIView alloc]initWithFrame:CGRectMake(0,0,320,480)];
[self.view addSubview:myview];
Then add this button to this view like 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);
[myview addSubview:button];
Try this.. if it is not working communicate with me...

how can I set actions on UIImages when I am using them as buttons?

Hello
I am using images as buttons so how can I put event methods as they pressed, my coding is as below ...
UIImageView* view = [[UIImageView alloc] initWithImage: [UIImage imageNamed: #"front_pag.png"]];
view.frame = CGRectMake(0, 0, 320, 460);
[self.view addSubview:view];
new_button = [[UIButton alloc] init];
UIImage *img1 = [UIImage imageNamed:#"new.png"];
[new_button setImage:img1 forState:UIControlStateNormal];
[new_button setFrame:CGRectMake(85, 430, 0, 0)];
[new_button addTarget:self action:#selector(newMethod)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:new_button];
what should I do, because in this case, I can't call newMethod, even I have this method in same class.
what should I do ?
You should change your addTarget call to this:
[new_button addTarget:self action:#selector(newMethod:)
forControlEvents:UIControlEventTouchUpInside];
Pay attention to semicolon after the newMethod: name
This is because the signature of your selector should be as following:
- (void) newMethod:(id) sender
This way it will be called.
Good luck
I think you can try this out,
uiimage *img1 = [UIImage imageNamed:#"someImage.png"];
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[button setImage:img1 forState:UIControlStateNormal]
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];

combining UISegmentedControl and UIBarButtonItem in navigation controller to customized barbuttonitem background image

I have a barbuttonitem that made programmatically above a navigation controller. I want to display it's title and background also a highlighted effect when i press that bar button.
Here's the code I use :
NSArray *segmentText = [segmentTextMutable copy];
UIImage *image = [[[UIImage alloc] init] autorelease];
image = [UIImage imageNamed:#"bunga.jpg"];
_docSegmentedControl = [[UISegmentedControl alloc] initWithItems:segmentText];
_docSegmentedControl.selectedSegmentIndex = 0;
_docSegmentedControl.autoresizingMask = UIViewAutoresizingFlexibleHeight;
_docSegmentedControl.segmentedControlStyle = UISegmentedControlStyleBezeled;
[_docSegmentedControl addTarget:self action:#selector(docSegmentAction:) forControlEvents:UIControlEventValueChanged];
[_docSegmentedControl setBackgroundColor:[UIColor colorWithPatternImage:image]];
UIView *barBackground = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
UIButton *barButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *buttonImage = [UIImage imageNamed:#"button.png"];
UIImage *buttonPressedImage = [UIImage imageNamed:#"buttonPressed.png"];
[[barButton titleLabel] setFont:[UIFont boldSystemFontOfSize:12.0]];
[barButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[barButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
[barButton setTitleShadowColor:[UIColor colorWithWhite:1.0 alpha:0.7] forState:UIControlStateNormal];
[barButton setTitleShadowColor:[UIColor clearColor] forState:UIControlStateHighlighted];
[[barButton titleLabel] setShadowOffset:CGSizeMake(0.0, 1.0)];
CGRect buttonFrame = CGRectMake(0, 0, 110.0, 40);
//buttonFrame.size.width = 110.0;
//buttonFrame.size.height = buttonFrame.size.height;
[barButton setFrame:buttonFrame];
[barButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[barButton setBackgroundImage:buttonPressedImage forState:UIControlStateHighlighted];
[barButton setTitle:docSegmentFileName forState:UIControlStateNormal];
[barButton addTarget:self action:#selector(docSegmentAction:) forControlEvents:UIControlEventTouchUpInside];
[barBackground addSubview:barButton];
UIBarButtonItem *segmentItem = [[UIBarButtonItem alloc] initWithCustomView:_docSegmentedControl];
self.navItem.leftBarButtonItem = segmentItem;
self.navItem.leftBarButtonItem.title = #"";
[self.navItem.leftBarButtonItem setCustomView:barBackground];
Unfortunately, this doesn't work. Instead of showing the UIBarButtonItem, it simply vanishes (it becomes 100% transparant). When I leave out the setCustomView method, the UIBarButtonItem appears, but is not customized. How can i solve this problem?
thank's...
You are removing the UISegmentedControl from the UIBarButtonItem when you call [self.navItem.leftBarButtonItem setCustomView:barBackground];. You need to modify the UISegmentedControl not the UIBarButtonItem.