I have a simple button and I want to put an image inside, so I have this code:
myButton = [[UIButton alloc] initWithFrame:CGRectMake(20, 13, 116, 138)];
UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[self.visuel lpath] ofType:#"jpg"]];
[myButton setImage:image forState:UIControlStateNormal];
in InterfaceBuilder Its easy to tell my image to take the size and width of my button but how to do
that programmatically? do I have to use myButton.contentHorizontalAlignment =..... something like that?
Best Regards,
What you probably want to do is this...
myButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
myButton.frame = CGRectMake(20.0, 13.0, 116.0, 138.0);
UIImage *tImage = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[self.visue1 lpath] ofType:#"jpg"]];
[myButton setBackgroundImage:[tImage stretchableImageWithLeftCapWidth:11 topCapHeight:0] forState:UIControlStateNormal];
[tImage release];
Of course the cap widths will depend on your image. It just repeats the next line of pixels over and over again until your image stretches to the proper width
Related
Currently I have 2 ways of displaying images in a cell, which way will help to table run smoothly, I'll use lazytable later? In each cell should the buttons be given a setimage or is adding an imageview with a set image just as good? Also in the first method I have to scale each image dynamically. Thank you!
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *img = [UIImage imageWithData:"png image from url"];
[button setImage:img forState:UIControlStateNormal];
/// Or
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *img = [UIImage imageWithData:"png image from url"];
UIImageView *imageView2 = [[UIImageView alloc] initWithImage:img];
[button2 addSubview:imageView2];
Since the button has an image property, why adding an image view to the button, its recommended to use approach 1
Whats wrong with the following piece of code:
UIButton *button = [[UIButton alloc]init];
CGRect frame = CGRectMake(180, 10, 33, 33);
button.frame = frame;
button.tag = 1001;
UIImage *image = [[[UIImage alloc]init]autorelease];
image = [UIImage imageNamed:#"icon.png"];
[button setBackgroundImage:image forState:UIControlStateNormal];
[image release];
[button release];
If this is wrong where does it need correction and why?
I see several problems:
You sent autorelease to image, then manually released it.
You released button, but you didn't add it as a subview to anything. So basically, you did all that for nothing.
You instantiate UIImage, then you do instantiate it again. in the next line: Instead just do:
UIImage *image = [UIImage imageNamed:#"icon.png"];
and delete UIImage *image = [[[UIImage alloc]init]autorelease]; and [image release]; statement.
You can try this
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(180, 10, 33, 33);;
button.tag = 1001;
UIImage *image = [UIImage imageNamed:#"icon.png"];
if (image == nil) {
NSLog(#"can't find icon.png");
} else {
[button setBackgroundImage:image forState:UIControlStateNormal];
}
//add button to the parent's view
Did you make sure image is not nil?
I have created a button using code shown below -
UIImage *kalenderImage = [UIImage imageNamed:#"start_icon_calendar_u.png"];
UIImageView *kalenderImageView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kalenderImage.size.width/2, kalenderImage.size.height/2)] autorelease];
[kalenderImageView setImage:kalenderImage];
UILabel* kalendarLabel = [[[UILabel alloc] initWithFrame:CGRectMake (0, kalenderImage.size.height/2-15, kalenderImage.size.width/2, kalenderImage.size.height/2)] autorelease];
kalendarLabel.text = #"Kalender";
[kalenderButton addSubview:kalenderImageView];
[kalenderButton addSubview:kalendarLabel];
[kalenderButton addTarget:self action:#selector(showCalendar:) forControlEvents:UIControlEventTouchUpInside];
I need to change the image of button for UIControlStateHighlighted state. How can i do this ?
I dont want to use
KalenderButton setBackgroundImage:#"" forState:]
[KalenderButton setImage:#"" forState]
Its very simple. Look at the following code. It checks the file is locked and changes the image of the button accordingly. You can edit the code according to your necessity.
if (fileLocked) {
UIImage *image = [UIImage imageNamed: [NSString stringWithFormat:#"icon-lock.png"]];
[lockButton setImage:image forState:UIControlStateNormal];
} else {
UIImage *image = [UIImage imageNamed: [NSString stringWithFormat:#"icon-unlock.png"]];
[lockButton setImage:image forState:UIControlStateNormal];
}
It works.
I have a transparent image which ("hold_empty.png", below) which I want to turn into a button. Inside the button there will be a view with a colored background which is slightly smaller size than the actual image to hold the background color.
The reason for this is because the image has rounded corners and so I cannot simply put a background color on the image as it will appear to be bigger than the image.
The image is a transparent square with "rounded corners". The effect I am trying to create should be like layers.
Background color layer (red, green, etc)
The "hold_empty.png" picture (above)
The whole object (including bg color layer) should be clickable.
The problem I am experiencing is that only the image (and its borders) appears to be clickable, and not the whole object.
The code follows below.
// x,y,w,h
CGRect frame = CGRectMake(10, 10, 72, 72);
CGRect frame2 = CGRectMake(5, 5, 60, 60); // I know this is not filling the image, its just a test
UIView *bgColorView = [[UIView alloc] initWithFrame:frame2];
[bgColorView setFrame:frame2];
bgColorView.backgroundColor = [UIColor redColor];
UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"hold_empty" ofType:#"png"]];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn addSubview:bgColorView];
[btn setImage:image forState:UIControlStateNormal];
btn.frame = frame;
[btn addTarget:self action:#selector(btnSelectColor:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
[bgColorView release];
So, to summarize: How do I make a transparent image button with a background color clickable?
Thanks.
The clickable area is generally the frame of the button which would not be clipped by its super-views, if you would let them clip contents.
The fact that you see something on screen doesn't mean it's "visible" in the sense that you can touch it. If you let every view clip its subviews (through IB, or by setting view.clipsToBounds = YES;) then any problems in that respect will show up clearly.
(Note that any UIButton, UIImageView, etc is a view and can be set to clip its contents)
Do you need to maybe enable user interaction on the subview?
bgColorView.userInteractionEnabled = YES;
I think I may have an answer that is working, but would like your comments/suggestions.
I created an UIImageView and put my background color layer and image layer inside that and then put the UIImageView inside the btn.
The only problem is now the button does not look like it is being pressed.
When I add the "setShowsTouchWhenHighlighted" option it has what looks like a big white star in the middle of the button when the user clicks ont he button.
// x,y,w,h
CGRect frame = CGRectMake(10, 10, 72, 72);
CGRect frame2 = CGRectMake(5, 5, 62, 62); // This fits, but may not be 100% accurate
// View
UIView *bgColorView = [[UIView alloc] initWithFrame:frame2];
[bgColorView setFrame:frame2];
bgColorView.backgroundColor = [UIColor redColor];
bgColorView.userInteractionEnabled = YES;
[bgColorView setNeedsDisplayInRect:frame2];
// Image
UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"hold_empty2" ofType:#"png"]];
UIImageView *imgView = [[UIImageView alloc] initWithImage:image];
[imgView insertSubview:bgColorView atIndex:0];
// Btn
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:frame];
[btn addSubview:imgView];
[btn setShowsTouchWhenHighlighted:YES];
[btn setUserInteractionEnabled:YES];
[btn addTarget:self action:#selector(btnSelectColor:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
[imgView release];
[bgColorView release];
I've cracked it now. One of the problems I was having was that it was not working with avatars (sometimes it would appear behind the layer, othertimes it would not show at all).
This is my solution.
UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"some_avatar" ofType:#"png"]];
UIImageView *imgView = [[UIImageView alloc] initWithImage:image];
CGRect fullFrame = CGRectMake(25, 10, 70,72);
CGRect frame = CGRectMake(28, 13, 63,65);
UIButton *h=[UIButton buttonWithType:UIButtonTypeCustom];
[h setFrame:fullFrame];
[[h layer] setMasksToBounds:YES];
//[h setBackgroundImage:image forState:UIControlStateNormal];
//[h setImage:image forState:UIControlStateNormal];
[h setShowsTouchWhenHighlighted:YES];
[h setClipsToBounds:YES];
CAGradientLayer *gradientLayer = [[CAGradientLayer alloc] init];
[gradientLayer setBounds:frame];
[gradientLayer setPosition:CGPointMake([h bounds].size.width/2, [h bounds].size.height/2)];
[gradientLayer setColors:[NSArray arrayWithObjects:
(id)[[UIColor darkGrayColor]CGColor],(id)[[UIColor blackColor] CGColor], nil]];
[[h layer] insertSublayer:gradientLayer atIndex:0];
[h insertSubview:imgView atIndex:1];
[h addTarget:self action:#selector(btnSelectColor:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:h];
[imgView release];
UIButton *ticketButtonObj=
[[UIButton alloc]initWithFrame:CGRectMake(140.0f 100.0f, 390.0f, 40.0f)];
UIImage *buttonicon1=[UIImage alloc];
buttonicon1=[UIImage imageNamed:#"Generalticket.png"];
how can set background of ticketButtonObj with buttonicon1 .. in left alignment ?
pls help me... thanks and regards by raju.
UIButton *ticketButtonObj = [[UIButton alloc] initWithFrame:CGRectMake(140.0f 100.0f, 390.0f, 40.0f)];
UIImage* buttonicon1 = [UIImage imageNamed:#"Generalticket.png"];
[ticketButtonObj setImage:image forState:UIControlStateNormal];
ticketButtonObj.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
or, if you want to set the background image:
[ticketButtonObj setBackgroundImage:image forState:UIControlStateNormal];
But you cannot set the alignment for the background.