iphone UIbutton with image click problem - iphone

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.

Related

how to set the scroll view contentsize dynamically?

I had an application in which i am setting the content size of the scroll view dynamically according to the number of buttons which i am creating from an array.like this `
for(int i =0;i<[sarray count];i++)
{
NSMutableDictionary *dicttable=[sarray objectAtIndex:i];
NSString *head=[dicttable objectForKey:#"cat"];
btn= [UIButton buttonWithType:UIButtonTypeCustom];
int j=i+1;
btn.frame = CGRectMake((j-1)*87,0,87, 44);
[btn setBackgroundImage:[UIImage imageNamed:#"bar.png"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:#"bar_hvr.png"] forState:UIControlStateSelected];
btn.backgroundColor = [UIColor clearColor];
btn.titleLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:14];
btn.titleLabel.textColor = [UIColor whiteColor];
[btn setTitle:head forState:UIControlStateNormal];
btn.tag = i;
[btn setShowsTouchWhenHighlighted:YES];
[Scroller addSubview:btn];
[btn addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
if(btn.tag==0)
{
sendActionsForControlEvents:UIControlEventTouchUpInside];
}
else
{
btn.selected=NO;
}
}
}
[Scroller setContentSize:CGSizeMake([sarray count]*85, 44)];
`
but here the problem is after the last button also the scroll view scrolls .it need not to be happend,i need the scrolling ends with last button on the view,can anybody help me on this
set scrollview bounces to NO
scrollView.bounces = NO;
yes as you said it looses the smoothness. But no other choice. If you found any sol please update the answer.
hey i am sending you complete example to make button dynamicay and seting the content size scrollview on number of count ,i had just given u one example code u have to set the size of frame of button as u requried
UIScrollView *scroll_View=[[UIScrollView alloc]initWithFrame:self.view.bounds];
[self.view addSubview:scroll_View];
int y=0;
for(int i =1;i<10;i++)
{
// NSMutableDictionary *dicttable=[sarray objectAtIndex:i];
// NSString *head=[dicttable objectForKey:#"cat"];
UIButton* btn= [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(y,89,100, 50);
[btn setBackgroundImage:[UIImage imageNamed:#"bar.png"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:#"bar_hvr.png"] forState:UIControlStateSelected];
btn.backgroundColor = [UIColor clearColor];
btn.titleLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:14];
NSString *str=[NSString stringWithFormat:#"%d",i];
btn.titleLabel.textColor = [UIColor whiteColor];
[btn setTitle:str forState:UIControlStateNormal];
btn.tag = i;
[btn setShowsTouchWhenHighlighted:YES];
[scroll_View addSubview:btn];
[btn addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
y=110*i;
}
[scroll_View setContentSize:CGSizeMake(y, 400)];
enjoy coding
You just declare one yMargin variable of type CGFloat and initialize it with some required value as 0. Now every time whenever you are adding a new subview vertically do y+=lbl.frame.size.height;
Now setContentSize:CGSizeMake([sarray count]*85, y margin+someRequiredMargin)];
(someRequiredMargin - you want to add below )

iOS Programmatically Created Button not Working

Hi I have a UIButton that I created programmatically but unfortunately its not working. When I click on the UIButton nothing happens. I don't know what is the problem. Any help would be appreciated.
UIButton *connectedStories = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//set the position of the button
connectedStories.frame = CGRectMake(10, 10, 1900, 30);
connectedStories.backgroundColor = [UIColor whiteColor];
[connectedStories setTitle:#"Button" forState:UIControlStateNormal];
[connectedStories setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[connectedStories addTarget:self action:#selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
[label setText:storyTitle];
[label1 setText:storyDescription];
[contentView addSubview:backGroundImageView];
[contentView addSubview:connectedStories];
[contentView addSubview:label];
[contentView addSubview:label1];
[viewContainer addSubview:contentView];
return contentView;
- (void)buttonClicked
{
NSLog(#"button clicked");
}
I think labels recives touches instead of button.
Add userInteractionEnabled = NO; to your labels.
label.userInteractionEnabled = NO;
label1.userInteractionEnabled = NO;
Try changing the top part of your code to this:
CGRect frame = CGRectMake(10, 10, 1900, 30);
UIButton *connectedStories = [[UIButton alloc] initWithFrame:frame];
//set the position of the button
[connectedStories setBackgroundColor:[UIColor whiteColor]];
[connectedStories setTitle:#"Button" forState:UIControlStateNormal];
[connectedStories setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[connectedStories addTarget:nil action:#selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
It may very well be the following:
[contentView addSubview:backGroundImageView];
[contentView addSubview:connectedStories];
[contentView addSubview:label];
[contentView addSubview:label1];
im sure you would need to add them in the following order:
[contentView addSubview:backGroundImageView];
[contentView addSubview:label];
[contentView addSubview:label1];
[contentView addSubview:connectedStories];
This is because when you call addSubview: it adds it to end of the receivers list of subViews and the most recent subView appears on top.
Check out the UIView Documentation here
the reason that the Interaction isn't being detected is that you are not passing the touchEvents through to the other subViews as they are being captured by label1 and stopping there.
By default the last subView captures all touch events and doesn't pass them on.
Add the colon in last of your method in selector
[connectedStories addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[label setText:storyTitle];
UIButton *custombutton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[custombutton addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[custombutton setTitle:#"Click" forState:UIControlStateNormal];
custombutton.frame = CGRectMake(80.0, 110.0, 160.0, 40.0);
custombutton.titleLabel.textColor = [UIColor colorWithRed: 2.0f/255.0f green: 155.0f/255.0f blue: 213.0f/255.0f alpha:1];
[custombutton setImage:[UIImage imageNamed:#"hh.png"] forState:UIControlStateNormal];
[view addSubview:custombutton];

How to change image of button?

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

How to set the buttons label text color for state UIControlStateHighlighted

I am creating an iPhone application in which i have a custom button. i have set the buttons title by creating a Label and adding it as subview. now when the button is highlighted i want to change the labels text color.
here is my code,
UIButton *button1= [UIButton buttonWithType:UIButtonTypeCustom];
[button1 setFrame:CGRectMake(68,162, 635, 101)];
[button1 setImage:[UIImage imageNamed:#"startwithouttext.png"] forState:UIControlStateNormal];
[button1 setImage:[UIImage imageNamed:#"startactivewithouttext.png"] forState:UIControlStateHighlighted];
UILabel *buttonLabel = [[UILabel alloc] initWithFrame:CGRectMake(button1.bounds.origin.x+50, button1.bounds.origin.y+20, button1.bounds.size.width-100, button1.bounds.size.height-40)];
[buttonLabel setFont:[UIFont fontWithName:#"Helvetica" size:28]];
buttonLabel.backgroundColor=[UIColor clearColor];
buttonLabel.textColor=[UIColor colorWithRed:83.0/255.0 green:83.0/255.0 blue:83.0/255.0 alpha:1.0];
buttonLabel.highlightedTextColor=[UIColor whiteColor];
buttonLabel.text = #"Long text string";
[button1 addSubview:buttonLabel];
[button1 bringSubviewToFront:buttonLabel];
[button1 setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[button1 setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
[button1 addTarget:self action:#selector(button1clicked:) forControlEvents:
[mainView button1];
can any body help me to change the text color when the button is highlighted?
Found the answer in a different question on StackOverflow:
UIButton color issues
[button1 setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
This is if you can work without creating a Label and adding it as subview as you mention above.
you can add target for UIControlStateHighlighted state of UIButton like
[button1 addTarget:self action:#selector(buttonHighlighted:) forControlEvents:UIControlStateHighlighted];
and in buttonHighlighted method you can change the color of your label's text
- (void) buttonHighlighted:(id)sender
{
//code here
}
Hope it gives you an idea.
For SelectedColor
[yourButton setTitleColor:[UIColor purpleColor] forState:UIControlStateSelected];
For HighlightedColor
[yourButton setTitleColor:[UIColor orangeColor] forState:UIControlStateHighlighted];

iPad - set text to a button programmatically

I'm new to this iPad business, and I'm creating some buttons inside a scrollview programmatically based on the contents of an XML file. I have this code on a for:
float x = (SLIDER_ELEMENT_HEIGHT * i) + 20;
CGRect frame = CGRectMake(x, 0, SLIDER_ELEMENT_WIDTH, SLIDER_ELEMENT_HEIGHT);
UIButton *button = [[UIButton alloc] initWithFrame:frame];
UIColor *bgColor = [[UIColor alloc] initWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
button.backgroundColor = bgColor;
NSString *titleForButton = [NSString stringWithFormat: #"This is my title"];
[button setTitle:titleForButton forState:(UIControlStateNormal | UIControlStateApplication | UIControlStateHighlighted)];
UIColor *fgColor = [[UIColor alloc] initWithRed:0.5 green:0.5 blue:0.5 alpha:1.0];
[button setTitleColor:fgColor forState:(UIControlStateNormal | UIControlStateApplication | UIControlStateHighlighted)];
[button addTarget:self action:#selector(buttonMethod:) forControlEvents:(UIControlEvents)UIControlEventTouchDown];
[scrl_lastIssues addSubview:button];
Now, the method listener I'm appending is working OK, but the text of the button never shows up... what am I doing wrong?
Try change the line:
[button setTitle:titleForButton forState:(UIControlStateNormal | UIControlStateApplication | UIControlStateHighlighted)];
to:
[button setTitle:titleForButton forState:UIControlStateNormal];
the forState parameter does not work the way you currently expected, it works like:
[button setTitle:#"Normal" forState:UIControlStateNormal];
[button setTitle:#"Highlighted" forState:UIControlStateHighlighted];
Also, if you only set the UIControlStateNormal text..it will be the text for all the control states that you have separated by bit wise ORs, which is, I believe, the functionality you are searching for.
[button setTitle:#"Return" forState:UIControlStateNormal];
[button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor orangeColor]];
And if you want to set the same title for all the states then do this:
[button setTitle:titleForButton forState:UIControlStateNormal];
[button setTitle:titleForButton forState:UIControlStateHighlighted];