Is it possible to add a UILabel to UITableView Cell which has already UIButton as Subview? - objective-c

Hii is this possible to add UILabel to UITableView Cell which has UIButton as Subview. I meant this UILabel must be added to the UIButton as the DetailLabel i.e below the title of the Button?
E.x My coding is that
UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
[button setImage:[UIImage imageNamed:#"launchpad.png"]forState:UIControlStateNormal];
[button setTag:001];
[button addTarget:self action:#selector(buttonPressed:)forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button];
Below the Title text i want the label as the subview to load the dynamic text at runtime.
Hope i explained clearly. Please help me with the coding and the way to add UILabel.

You can add this label to UIButton as subview:
UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
[button setImage:[UIImage imageNamed:#"launchpad.png"]forState:UIControlStateNormal];
[button setTag:001];
[button addTarget:self action:#selector(buttonPressed:)forControlEvents:UIControlEventTouchUpInside];
UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0,24, 320, 20)];
label.text = #"Some text"
[button addSubview:label];
[cell addSubview:button];

Related

ChildView didn't maintain its ParentView Frame

Hope all of you will be fine. Guys i have a situation, I have a uiview and uibutton as under:
UIView* containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 200, self.view.frame.size.width, 70)];
containerView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:containerView];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.backgroundColor = [UIColor redColor];
[button addTarget:self action:#selector(SettingPressed:) forControlEvents:UIControlEventTouchUpInside];
UIImage *image = [UIImage imageNamed:#"settings_button.png"];
[button setImage:image forState:UIControlStateHighlighted & UIControlStateNormal & UIControlStateSelected];
button.frame = CGRectMake(0,0, 40, 40);
button.center = containerView.center;
[containerView addSubview:button];
The problem is uibutton appear far from uiview instead of in the middle of uiview. What i am mistaking, please guide me.
Thanks.
First, when you change a view's center property the frame property will be changed automatically so you only should set the button center:
button.center = CGPointMake(containerView.frame.size.width / 2,
containerView.frame.size.height / 2);
Now, whenever you change the center property you need to manually tell the view to redraw itself using setNeedsDisplay :
[button setNeedsDisplay]
and lastly, the difference between frame and bounds that frame defines the position and size relative to the parent view while bounds describes the drawing area inside (or outside) the frame.

Objective-c : action:#selector(xxxxxx) not called

I'm currently trying to add a button to my app. If the user push on it, it should display an alert.
I want to add the button to a section header of a tableview. Here is my code in tableView:viewForHeaderInSection:
UIImageView *headerView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 20)] autorelease];
...
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(180, 4, 15, 15)];
UIImage *img=[UIImage imageNamed:#"myimg"];
[button setImage:img forState:UIControlStateNormal];
[button addTarget:self action:#selector(showMyAlert) forControlEvents:UIControlEventTouchUpInside];
[headerView addSubview:button];
return headerView;
And in the same class, I have the method :
-(void)showMyAlert{
NSLog(#"HERE SHOW ALERT");
...
}
The button is correctly displayed but the showMyAlert method is never called when I push on it.
Does anyone have an idea of what is wrong with my code ?
Thanks if you could help me :)
Change your UIImageView as UIView and return it as the header view.
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 20)] autorelease];
Make sure img is not nil. If that is nil, your button will have no image and you will see nothing.
Modified below lines of code, Try like this:-
[button addTarget:self action:#selector(showMyAlert:) forControlEvents:UIControlEventTouchUpInside];
-(IBAction)showMyAlert:(id)sender{
NSLog(#"HERE SHOW ALERT");
...
}
Try this, in viewDidLoad,
-(void)viewDidLoad
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(XXX, YYY, XXX, YYY)];
UIImageView *imageView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 20)] autorelease];
[headerView addSubview:imageView];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(180, 4, 15, 15)];
UIImage *img=[UIImage imageNamed:#"myimg"];
[button setImage:img forState:UIControlStateNormal];
[button addTarget:self action:#selector(showMyAlert) forControlEvents:UIControlEventTouchUpInside];
[headerView addSubview:button];
self.tableView.tableHeaderView = headerView;
}

Change UIToolbar Button Image from Code

is there any way to change the button image inside UIToolbar?
I am actually using this way but it doesn't work.
[myButton setImage:[UIImage imageNamed:#"blue.png"]];
For your information, "myButton" is an IBOulet UIBarButtonItem.
#property (nonatomic, strong) IBOutlet UIBarButtonItem *myButton;
Use the following cole. i hope it will use to you.
UIImage *image = [UIImage imageNamed:#"buttonImage.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.bounds = CGRectMake( 0, 0, image.size.width, image.size.height );
[button setImage:image forState:UIControlStateNormal];
[button addTarget:myTarget action:#selector(myAction) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
Thanks,.
// add images to bar button item of toolbar in iphone
to set button type as custum:
button=[UIButton buttonWithType: UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"myImage.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 40, 40)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
// add the image on toolbar:
[toolbar insertSubview:[[[UIImageView alloc]
initWithImage:[UIImage imageNamed:#"image.png"]] autorelease] atIndex:1];

UIButton to show BackgroundImage, Image and Title

I want to create a UIButton that uses its setBackgroundImage, setImage, and setTitle properties, but when I add setTitle in combination with setImage and setBackgroundImage the title doesn't appear.
I've tried the following.
[button setBackgroundImage:[UIImage imageNamed:#"image3.jpg"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"month_pct_general.png"] forState:UIControlStateNormal];
[button setTitle:[NSString stringWithFormat:#"%d",[button tag]] forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button.titleLabel setFont:[UIFont boldSystemFontOfSize:20.0f]];
[button setTitleEdgeInsets:UIEdgeInsetsMake(45, 0, 0, 25)];
your title on button and on title your all image it there so add label on the image like this.
UIButton *myButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
[myButton setFrame:CGRectMake(0,3,70,32)];
[myButton setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(compete) forControlEvents:UIControlEventTouchUpInside];
UILabel *Label=[[UILabel alloc]initWithFrame:CGRectMake(6, 5, 60, 20)];
[Label setText:#"label"];
Label.backgroundColor=[UIColor clearColor];
[Label setFont:[UIFont fontWithName:#"Helvetica-Bold" size:12]];
Label.textColor=[UIColor whiteColor];
[myButton addSubview:Label];
your all image above the title only so your title is in background so use label on image view.
If you are making GUI using interface builder, then click on the label and go to Editor-> Arrange -> Send to Front.

UIButton on UIScrollView can't work

UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 180, 180)];
myLabel.backgroundColor = [UIColor greenColor];
[self.view addSubview:myLabel];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self addTarget:self action:#selector(show) forControlEvents:UIControlEventTouchDown];
myButton.frame = CGRectMake(80.0, 120.0, 30, 30);
myButton.showsTouchWhenHighlighted = YES;
[self addSubview:myButton];
in this way the button works(call show:), but use [myLabel addSubview:myButton]; the button doesn't work. not sure why?
----------------EDIT & SOLUTION------------
Thanks #KennyTM
UILabel by default doesn't handle any events. You need to set the userInteractionEnabled property of the label to YES.
Also you'd better not add a UIButton on the top of a UILabel.
UILabel by default doesn't handle any events. You need to set the userInteractionEnabled property of the label to YES.
A button shouldn't be a subview of a label anyway, it's illogical. Make both of them subviews of a UIView.