When i press button it changes image into green but it quit dark,but its original color like lookup button image,so what's wrong in my code?
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *clear_img = [UIImage imageNamed:#"0.png"];
UIImage *clear_hover_img = [UIImage imageNamed:#"0_hover.png"];
clear=[[UIButton alloc]init];
[clear setTitle:#"Clear" forState:UIControlStateNormal];
//clear.tag =12;
clear.titleLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:32];
if(clear.selected=TRUE){
[clear setBackgroundImage:clear_img forState:UIControlStateSelected];
[clear setBackgroundImage:clear_hover_img forState:UIControlStateNormal];
}
[clear setFrame:CGRectMake(1, 360, 159, 50)];
[clear addTarget:self action:#selector(clearInput)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:clear];
}
-(void)clearInput {
textfield.text = #"";
UIImage *lookup_img = [UIImage imageNamed:#"0_.png"];
[lookup setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[lookup setBackgroundImage:lookup_img forState:UIControlStateNormal];
}
For state TouchUpInside, Button Image Opacity decrease to 0.5.
try this-
if(clear.selected == TRUE){
[clear setBackgroundImage:clear_img forState:UIControlStateSelected];
[clear setBackgroundImage:clear_hover_img forState:UIControlStateNormal];
[clear setBackgroundImage:clear_hover_img UIControlStateHighlighted];
}
I think you wants to set Clear button Image as Lookup button. you had done with that but when you press on clear button the image get some darken.
If my understanding is right then you had done:
[clear setBackgroundImage:clear_img forState:UIControlStateSelected];
[clear setBackgroundImage:clear_hover_img forState:UIControlStateNormal];
add this line too:
[clear setBackgroundImage:clear_img forState:UIControlStateHighlighted];
Hope this helped
Related
How I can set UIImage in the normal state and set title in the selected state in my UIButton?
Hi, I try to set a UIImage in the normal state of UIButton, but when set an UImage in normal state this change the title of UIButton in the selected state changing the title for the image . I am using
myButton setTitle:content forState:UIControlStateSelected
in the state selected.
How I can set UIImage in the normal state and set title in the selected state in my UIButton?
PDT: sorry for my english!.
Try This:
BOOL selected;
-(IBAction)btnClicked:(UIButton *)sender{
if(selected)
{
[chkBtn setTitle:#"selected" forState:UIControlStateNormal];
chkImage.image=[UIImage imageNamed:#" "];
NSLog(#"%#",chkBtn.titleLabel);
selected=NO;
}
else
{
[chkBtn setTitle:#" " forState:UIControlStateNormal];
chkImage.image=[UIImage imageNamed:#"button_minus_pink.png"];
selected=YES;
}
}
// set with UIColorĀ
[self.button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
// set with UIImage
[self.button setBackgroundImage:[UIImage imageNamed:#"BlackRightBtn.png"] forState:UIControlStateNormal];
For Button Selected
if (self.button.isSelected == YES)
{
[self.button setTitle:#"Your_ButtonTitle" forState:UIControlStateNormal];
}
add your code like this:
[myButton setTitle:#"Your String" forState:UIControlStateSelected];
[myButton setImage:[UIImage imageNamed:#"img.png"] forState:UIControlStateSelected];
[myButton setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[myButton setTitle:#"" forState:UIControlStateNormal];
where img.png should not be there in your bundle
you can set this through Interface Builder as well.
You can define button and setImage for normal and selected state. Like this way.
buttonJobs = [UIButton buttonWithType:UIButtonTypeCustom ];
buttonJobs.tag = 1;
[buttonJobs setTitle:#"Jobs" forState:UIControlStateNormal];
buttonJobs.frame = CGRectMake(2, 56, 109, 33);
[buttonJobs setImage:[UIImage imageNamed:#"tabButtonImage_Off.png"] forState:UIControlStateNormal];//buttonJobs.png
[buttonJobs setImage:[UIImage imageNamed:#"tabButtonImage_On.png"] forState:UIControlStateDisabled];
[buttonJobs addTarget:self action:#selector(btnNextTab:) forControlEvents:UIControlEventTouchUpInside];
And in button selector method you can set your button selected according sender's tag.
-(void)btnNextTab:(UIButton*)sender
{
if(sender.tag == 1)
{
sender.selected = YES;
}
}
And for Title you can change color of text.
Hope this will help you.
Check with this code:
[myButton setTitle:content forState:UIControlStateSelected];
[myButton setImage:nil forState:UIControlStateSelected];
[myButton setImage:yourImage forState:UIControlStateNormal];
Try this code:
[myButton setTitle:#"Your String" forState:UIControlStateSelected];
[myButton setImage:nil forState:UIControlStateSelected];
[myButton setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[myButton setTitle:#"" forState:UIControlStateNormal];
I am created two buttons that are right next to each other to mimic a segmented control. I am doing this to customize the appearance beyond what the UIKit allows. I decided to use the selected property to keep a button pressed. I have two images that one for each state normal and selected.
The problem is that when I select a button, the button highlights and turns dark, because of the hightlight state. I decided to use the selected image for the highlight state too, but it flashes, any ideas or suggestions.
- (void)leftSegmentPressed:(id)sender
{
if ([sender isSelected]) {
[sender setSelected:NO];
}
else {
[sender setSelected:YES];
}
}
For the "selected" button, disable it and manually switch the image for the state.
- (void) viewDidLoad
{
[rightSegmentButton setImage:[UIImage imageNamed:#"unselected.png"] forState:UIControlStateNormal];
[rightSegmentButton setImage:[UIImage imageNamed:#"selected.png"] forState:UIControlStateDisabled];
[leftSegmentButton setImage:[UIImage imageNamed:#"unselected.png"] forState:UIControlStateNormal];
[leftSegmentButton setImage:[UIImage imageNamed:#"selected.png"] forState:UIControlStateDisabled];
}
- (void)leftSegmentPressed:(id)sender
{
sender.enabled = NO;
rightSegmentButton.enabled = YES;
}
- (void)rightSegmentPressed:(id)sender
{
sender.enabled = NO;
leftSegmentButton.enabled = YES;
}
Check whether the image you given is in your Bundle or check image name you given is in lower case or not. Then write like
[button1 setImage:[UIImage imageNamed:#"normal1.png"] forState:UIControlStateNormal];
[button1 setImage:[UIImage imageNamed:#"selected1.png"] forState:UIControlStateSelected];
[button2 setImage:[UIImage imageNamed:#"normal2.png"] forState:UIControlStateNormal];
[button2 setImage:[UIImage imageNamed:#"selected2.png"] forState:UIControlStateSelected];
button1.tag = 1;
button2.tag = 2;
[button1 addTarget:self action:#selector(buttonSelected:) forControlEvents:UIControlEventTouchUpInside]
[button2 addTarget:self action:#selector(buttonSelected:) forControlEvents:UIControlEventTouchUpInside]
in your button event method
-(void)buttonSelected:(id)sender {
if([sender tag] == 1) {
button1.selected = YES;
button2.selected = NO;
} else {
button1.selected = NO;
button2.selected = YES;
}
}
[button setAdjustsImageWhenHighlighted:NO];
This will prevent the flicker.
UIButton *yourButton1 = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
yourButton1.frame = CGRectMake(110.0, 360.0, 100.0, 30.0);
[yourButton1 setTitle:#"Left" forState:UIControlStateNormal];
yourButton.backgroundColor = [UIColor clearColor];
yourButton1.tag = 1;
[yourButton1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal ];
UIImage *buttonImageNormal = [UIImage imageNamed:#"yourNormalImage.png"];// set normal image
UIImage *strechableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[yourButton1 setBackgroundImage:strechableButtonImageNormal forState:UIControlStateNormal];
UIImage *buttonImagePressed = [UIImage imageNamed:#"yourSelectedImage.png"];// set selected image
UIImage *strechableButtonImagePressed = [buttonImagePressed stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[yourButton1 setBackgroundImage:strechableButtonImagePressed forState:UIControlStateHighlighted];
[yourButton1 addTarget:self action:#selector(leftSegmentPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:yourButton1];
do same for another second button.
and in action method set bellow code..
- (void)leftSegmentPressed:(id)sender
{
UIButton *btnTemp = (UIBUtton *)sender;
if (btnTemp.tag == 1) {
[yourButton1 setSelected:YES];
[yourButton2 setSelected:NO];
}
else {
[yourButton1 setSelected:NO];
[yourButton2 setSelected:YES];
}
}
I am new to iPhone,
xpos=30;
ypos=40;
for(int i=0;i<[MyArray count];i++)
{
if(i!=0)
{
if (i%4==0)
{
ypos+=180;
xpos=30;
}
else
{
xpos+=200;
}
}
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(xpos, ypos, 110.0, 130.0);
[button setImage: [UIImage imageNamed:#"ibook2.png"] forState:UIControlStateNormal];
[button setTitleEdgeInsets:UIEdgeInsetsMake(ypos, xpos, 0.0, 0.0)];
[button setTitle:[NSString stringWithFormat:#"%#", [MyArray objectAtIndex:i]] forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[self.view addSubview:button];
}
i am able to see button but unable to see my text on the UIButton.
Any help will be appreciated.
Please set buttons background image.You have set buttons image.
Please use this:
[button setBackgroundImage:[UIImage imageNamed:#"blue_button.png"]
forState:UIControlStateNormal];
For Wrapping button title User:
[button setTitle:#"Test button name 123" forState:UIControlStateNormal];
button.titleLabel.numberOfLines=2;
button.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
You can't see the text because you have used setImage: function of button. At a time you can display image or button on text but you can add image as a background so you can see both image and tittle. In short set image as a background.
[btn setBackgroundImage:[UIImage imageNamed:#"imageName"] forState:UIControlStateNormal];
Please also remove the line of setting inset.
Rather then setting
[button setImage: [UIImage imageNamed:#"ibook2.png"] forState:UIControlStateNormal]; // This will not allow you to set Button title.
You have to use :
[button setBackgroundImage: [UIImage imageNamed:#"ibook2.png"] forState:UIControlStateNormal]; // This will allow you to set title along background button image.
if you image is too big then it will only show the image and text get truncated due to image as text will be visible after image, try using setBackground image or try small resolution image or setImageEdgeInsets .
I have 3 UIbuttons in my with background image in my app...
for all those 3 buttons if images are assigned only lower half portion of button is clickable and upper half doesn't work at all..
any idea why ?
I tried to put color for buttons to check if they have right CGRect but it looks proper...
Here is some code
UIButton* segment=[[UIButton alloc] initWithFrame:CGRectMake(0+(i* segmentWidth) , 0,segmentWidth, 34)];
segment.backgroundColor = [UIColor redColor];
[segment addTarget:self action:#selector(segmentActionButton:) forControlEvents:UIControlEventTouchUpInside];
[segment setTag:i];
segment.titleLabel.font=[UIFont fontWithName:#"Arial" size:12.0];
segment.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
[segment setTitle:[mItems objectAtIndex:i] forState:UIControlStateNormal];
[segment setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[segment setBackgroundImage:[UIImage imageNamed:SEGMENT_IMAGE_INACTIVE_MAP] forState:UIControlStateNormal];
[segment setBackgroundImage:[UIImage imageNamed:SEGMENT_IMAGE_ACTIVE_MAP] forState:UIControlStateHighlighted];
[self addSubview:segment];
[mButtonArray addObject:segment];
[segment release];
Hope it helps..
May be the error occurs because you are creating button in alloc+initWithFrame way. I'm not sure but try to create button in such way:
UIButton* segment = [UIButton buttonWithType:UIButtonTypeCustom];
segment.frame = CGRectMake(0+(i* segmentWidth) , 0,segmentWidth, 34);
And remove line [segment release]; as you will create autoreleased object.
I have an iPhone UIButton (Custom) which has a background image and text.
When touched, the image darkens (Good) but the text goes from the set Black to white.
How do I keep the text the same black so that when the button is touched, only the image changes color.
Most of the times, the following line will do:
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
If nothing happens, then use either this:
[button setTitleColor:[UIColor blackColor] forState:(UIControlStateSelected | UIControlStateHighlighted | UIControlStateNormal)];
Or this will do the trick:
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
See comments below for reason this answer was edited/expanded to include the 2 separate lines.
[button setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted]
okButton.titleLabel.textColor = [UIColor redColor];
[okButton addTarget:self action:#selector(isPressingForgetButton:) forControlEvents:UIControlEventTouchDown];
[okButton addTarget:self action:#selector(didPressForgetButton:) forControlEvents:UIControlEventTouchUpInside];
- (void) isPressingForgetButton:(id)sender
{
UIButton * bt = (UIButton *)sender;
bt.titleLabel.textColor = [UIColor greenColor];
}
- (void) didPressForgetButton:(id)sender
{
UIButton * bt = (UIButton *)sender;
bt.titleLabel.textColor = [UIColor redColor];
[self gotoUnblockView];
}