I am trying to create a UIButton - this works fine when I am using the simulator, but when I try on a device the button does not appear.
[self.followButton setImage:[UIImage new] forState:UIControlStateNormal];
[self.followButton setTitle:#"EDIT" forState:UIControlStateNormal];
self.profMoreButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *icon = [[UIImage imageNamed:#"More"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[self.profMoreButton.imageView setContentMode:UIViewContentModeScaleAspectFit];
[self.profMoreButton setImage:icon forState:UIControlStateNormal];
[self.profMoreButton.imageView setTintColor:[UIColor Primary]];
[self.profMoreButton addTarget:self action:#selector(morePressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:self.profMoreButton];
[self.profMoreButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.equalTo(self.followButton.mas_height).multipliedBy(0.6);
make.centerY.equalTo(self.followButton);
make.left.equalTo(self.followButton.mas_right).with.offset(8);
}];
if (!self.profMoreButton) {
NSLog(#"WHY!?????");
}
On my iPhone 6 the NSLog at the end is always printed and the button doesnt appear UNLESS I put a breakpoint at the top and step my way through.
self.profMoreButton was declared as a weak IBOutlet, I removed the IBOutlet and changed it to strong. Now it works 😳
I THINK YOU DONT GIVEN ALLOC UIBUTTON
settingsButton =[UIButton buttonWithType:UIButtonTypeCustom];
settingsButton.frame= CGRectMake(SCREEN_WIDTH-[UIImage imageNamed:#"setting.png"].size.width-30, 22, [UIImage imageNamed:#"setting.png"].size.width, [UIImage imageNamed:#"setting.png"].size.height);
[settingsButton setImage:[UIImage imageNamed:#"setting.png"] forState:UIControlStateNormal];
[settingsButton addTarget:self action:#selector(settingButtonMethod) forControlEvents:UIControlEventTouchUpInside];
[settingsButton setMultipleTouchEnabled:NO];
settingsButton.exclusiveTouch=YES;
settingsButton.backgroundColor=CLEAR_COLOR;
[self.view addSubview:settingsButton];
Related
When I define the button on .h, it works but when I code the button does not work.
- (void)viewDidLoad
{
[super viewDidLoad];
UIImage *buttonImage = [UIImage imageNamed:#"nl.png"];
//create the button and assign the image
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame=CGRectMake(230,302,32,32);
[button setImage:buttonImage forState:UIControlStateNormal];
//create a UIBarButtonItem with the button as a custom view
//UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
[button addTarget:self action:#selector(clickActionItem) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void)btnClicked
{
[self.button Sethidden:YES]:
self.button.hidden=YES;
_button.hidden=YES;
}
But not work
Issue
The problem is that you are creating UIButton on viewDidLoad Method and you don't keep the instance stored so you cannot access it. Another issue is that you are giving another method to selector of the button.
Solution
Change your target at selector and add the UIButton as parameter just like the code below
[button addTarget:self action:#selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
And the method btnClicked take the instance of the button which is passed at button
-(void)btnClicked:(UIButton *)button
{
[button sethidden:YES];
}
This should do the trick.
#implementation YourView{
UIButton *button;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIImage *buttonImage = [UIImage imageNamed:#"nl.png"];
//create the button and assign the image
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame=CGRectMake(230,302,32,32);
[button setImage:buttonImage forState:UIControlStateNormal];
//create a UIBarButtonItem with the button as a custom view
//UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
[button addTarget:self action:#selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void)btnClicked
{
[button setHidden:YES];
}
Your code did not give sufficient information for to see what you intend to do. Can you also provide the header file?
I have multiple of buttons.
They all load in the viewDidLoad. I want it so once one button has been pressed, it changes to the other image.
So Img1 loads via the viewDidload, then once it's been pressed, it goes to Img2
This is my current code which i have tried:
In the viewdidload:
btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn1 setFrame:CGRectMake(367, 117, 97, 25)];
[btn1 setImage:[UIImage imageNamed:#"img1.png"] forState:UIControlStateNormal];
[btn1 addTarget:self action:#selector(playSound:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];
In the ibaction (which i tried, but im guessing)
-(IBAction)ButtonAction
{
if (btn1==0)
{
btn1=1;
[btn1 setImage:[UIImage imageNamed:#"img1.png"] forState:UIControlStateNormal];
}
else
{
[btn1 setImage:[UIImage imageNamed:#"img2.png"] forState:UIControlStateNormal];
btn1=0;
}
}
Thanks for the help
You need to make sure you are hooking up your UIButton to the proper selector. Also, I would use an #property or a global / instance variable to track which image is currently in the UIButton. Here's some code that should do the trick:
//... in #interface
#property (nonatomic, strong) NSString *btn1ImageName;
- (void)viewDidLoad {
[super viewDidLoad];
// ...
self.btn1ImageName = #"img1.png";
btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn1 setFrame:CGRectMake(367, 117, 97, 25)];
[btn1 setImage:[UIImage imageNamed:self.btn1ImageName] forState:UIControlStateNormal];
[btn1 addTarget:self action:#selector(toggleImage:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];
}
- (void)toggleImage:(UIButton *)btn1 {
self.btn1ImageName = ([self.btn1ImageName isEqualToString:#"img1.png"]) ? #"img2.png" : #"img1.png";
[btn1 setImage:[UIImage imageNamed:self.btn1ImageName] forState:UIControlStateNormal];
}
if (btn1==0)
I don't get that part.
One easy solution would be to have two different images for a selected and un-selected state.
btn1.selected = YES, will automatically choose the selected button image.
btn1.selected = NO, will automaticall choose the un-selected button image.
I use the following code to load an image is on the upper, lower half is the title of the button, but only the image, is this why? Have any good Suggestions please let us know, thank you
UIButton *myButton = [[UIButton alloc]initWithFrame:CGRectMake(160, 400, 73, 44)];
[myButton setImage:[UIImage imageNamed:#"icon.png"] forState:UIControlStateNormal];
[myButton setImageEdgeInsets: UIEdgeInsetsMake(0,0,22,0)];
[myButton setTitle:#"test" forState:UIControlStateNormal];
[myButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[myButton setTitleEdgeInsets: UIEdgeInsetsMake(22,10,0,10)];
[self.view addSubview:myButton];
you can achieve this effect by this way also.. Try this ::
UIButton *myButton = [[UIButton alloc]initWithFrame:CGRectMake(160, 400, 73, 55)];
UIImage *img = [UIImage imageNamed:#"icon.png"];
UIImageView *imgVw = [[UIImageView alloc ] init];
imgVw.image = img;
imgVw.frame = CGRectMake(myButton.frame.origin.x, myButton.frame.origin.y, myButton.frame.size.width, (myButton.frame.size.height-18));
[myButton setTitle:#"test" forState:UIControlStateNormal];
[myButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[myButton setTitleEdgeInsets: UIEdgeInsetsMake(34,10,0,10)];
[myButton addTarget:self action:#selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:imgVw];
[self.view addSubview:myButton];
Your button method
-(void) btnClick:(id)sender
{
NSLog(#"working");
}
You can achieve from your logic also :: Here i've edited some code in your logic
UIButton *myButton = [[UIButton alloc]initWithFrame:CGRectMake(160, 400, 40, 35)];
[myButton setImage:[UIImage imageNamed:#"icon.png"] forState:UIControlStateNormal];
[myButton setImageEdgeInsets:UIEdgeInsetsMake(0,(myButton.frame.size.width-52),17,0)];
[myButton setTitle:#"test" forState:UIControlStateNormal];
[myButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[myButton setTitleEdgeInsets: UIEdgeInsetsMake(22,(myButton.frame.size.width-78),0,22)];
[self.view addSubview:myButton];
but, in this logic you have to resize your image to 23*23 pixels. according to your image size UIEdgeInsetsMake's parameter will be changed.
Hope this helps. Happy coding.
I put in a tolbar a bar button item, and I set it with a png (first.png) and when I push it I want to change its png in "second.png"
This code doesn't work fine:
UIImage *first = [UIImage imageNamed:#"first.png"];
UIImage *second = [UIImage imageNamed:#"second.png"];
if ([sender isSelected])
{
[sender setImage:first forState:UIControlStateNormal];
[sender setSelected:NO];
}
else
{
[sender setImage:second forState:UIControlStateSelected];
[sender setSelected:YES];
}
The current answerers do not realize that UIBarButtonItem does NOT inherit from UIButton, so setImage:forState: will most definitely not work. UIBarButtonItems cannot be set for different states. You can, however, utilize something like this (declared in the UIBarItem docs):
sender.image = [UIImage imageNamed:#"first.png"];
I'm not clear on why you have an if block in there. Can't you initialize both state images as a one-off initialization step? I.e.
[sender setImage:[UIImage imageNamed:#"first.png"] forState:UIControlStateNormal];
[sender setImage:[UIImage imageNamed:#"second.png"] forState:UIControlStateSelected];
In fact, this can probably be done in Interface Builder if you don't need to do it programmatically.
Just write after creation of button this lines:
UIImage *first = [UIImage imageNamed:#"first.png"];
UIImage *second = [UIImage imageNamed:#"second.png"];
[sender setImage:first forState:UIControlStateNormal];
[sender setImage:second forState:UIControlStateSelected];
You don't need if-else statement.
Updated:
CGRect myFrame;
UIButton *myButton = [[UIButton alloc] initWithFrame:myFrame];
[myButton setImage:[UIImage imageNamed:#"first.png"] forState:UIControlStateNormal];
[myButton setImage:[UIImage imageNamed:#"second.png"] forState:UIControlStateSelected];
UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:myButton];
[myButton release];
I would like to create a button with a custom image ( background image), i have done this.
how can assign the imageview to may button ?
thanks for your answers
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(goToGeoloc)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(245.0, 8.0, 45.0, 45.0);
UIImage *image = [UIImage imageNamed:#"ico_plan.png"];
UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
What you want is:
[button setBackgroundImage:image forState:UIControlStateNormal];
or
[button setImage:image forState:UIControlStateNormal];
#izan you dont need a separate UIImageView to show on the UIButton.
A UIButton comes with 2 UIImageView inside it. you can just set the image for these 2 imageViews.
You can set the image for the first imageview using setImage:forState: method. By doing this you cannot show any title for that button. If you want to show title as well as image on the button you should use setBackgroundImage:forState: method.
Use the setImage method;
- (void)setImage:(UIImage *)image forState:(UIControlState)state
So your code would be;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:#selector(goToGeoloc) forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(245.0, 8.0, 45.0, 45.0);
UIImage *image = [UIImage imageNamed:#"ico_plan.png"];
[button setImage:image forState:UIControlStateNormal];
Or I you still want your text to be shown use;
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state
Which would result in;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:#selector(goToGeoloc) forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(245.0, 8.0, 45.0, 45.0);
UIImage *image = [UIImage imageNamed:#"ico_plan.png"];
[button setBackgroundImage:image forState:UIControlStateNormal];
UIButton documentation;
http://bit.ly/kruq5y
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(goToGeoloc)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"ico_plan.png"] forState:UIControlStateNormal];
button.frame = CGRectMake(245.0, 8.0, 45.0, 45.0);
Hope this helps