if ([cell.textLabel.text isEqualToString:#"Button"])
{
UIView *ButtonView = [[UIView alloc] initWithFrame:CGRectMake(100, 5, 100, 25)];
UIButton *Button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
Button.frame = CGRectMake(90, 5, 90, 30);
[Button setTitle:#"PopOver" forState:UIControlStateNormal];
[ButtonView addSubview:Button];
[cell.contentView addSubview:ButtonView];
[Button addTarget:self action:#selector(showPickerPopupAction)forControlEvents:UIControlEventTouchUpInside];
Button.tag = indexPath.row;
}
- (void) showPickerPopupAction
{
NSLog( #"Button clicked." );
}
The Button's frame is almost completely outside the ButtonView's frame. If you set a backgroundColor on the ButtonView, you'll see that only the top-left corner of the button is in the ButtonView frame and only that corner will respond to touches.
Adjust the size of the ButtonView frame or the size and position of the Button frame so that the button is completely inside ButtonView.
Not sure why you need ButtonView in the first place. You could just add Button directly to the contentView.
Also, you should do [ButtonView release] after adding ButtonView to the cell.contentView.
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"ButtonCell"] autorelease];
UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 44.0f)];
backgroundView.backgroundColor = [UIColor groupTableViewBackgroundColor];
cell.backgroundView = backgroundView;
UIButton *buttonLeft = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[buttonLeft setTitle:#"Left" forState:UIControlStateNormal];
[buttonLeft setFrame: CGRectMake( 10.0f, 3.0f, 145.0f, 40.0f)];
[buttonLeft addTarget:self action:#selector(addToFavorites) forControlEvents:UIControlEventTouchUpInside];
UIButton *buttonRight = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[buttonRight setTitle:#"Right" forState:UIControlStateNormal];
[buttonRight setFrame: CGRectMake( 165.0f, 3.0f, 145.0f, 40.0f)];
[buttonRight addTarget:self action:#selector(addToFavorites) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:buttonLeft];
[cell addSubview:buttonRight];
[backgroundView release];
-(void) addToFavorites {}
Works fine !!
Related
I want to display Image,Title name,and two left right buttons. That I have done successfully.
Here is the code:
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 100)];
UIButton *leftButton = [[UIButton alloc]init];
[leftButton setImage:[UIImage imageNamed:#"previous-arrow.png"] forState:UIControlStateNormal];
[leftButton addTarget:self action:#selector(goPrevious:) forControlEvents:UIControlEventTouchUpInside];
[leftButton setFrame:CGRectMake(20, 50, 14, 13)];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Title.png"]];
[imageView setFrame:CGRectMake(leftButton.frame.origin.x+leftButton.frame.size.width+4, 20, 50, 50)];
UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(imageView.frame.origin.x+imageView.frame.size.width+4, 30, 100, 40)];
NSString *cName = [NSString stringWithFormat:#"%#",[NameArray objectAtIndex:indexPath.section]];
[nameLabel setTextColor:[UIColor whiteColor]];
[nameLabel setBackgroundColor:[UIColor clearColor]];
[nameLabel setContentMode:UIViewContentModeScaleAspectFill];
[nameLabel setTextAlignment:UITextAlignmentLeft];
CGSize expectedLabelSize = [cName sizeWithFont:nameLabel.font
constrainedToSize:CGSizeMake(100, 40)
lineBreakMode:nameLabel.lineBreakMode];
[nameLabel setFrame:CGRectMake(imageView.frame.origin.x+imageView.frame.size.width+4, 35, expectedLabelSize.width, expectedLabelSize.height)];
nameLabel.text=cName;
UIButton *rightButton = [[UIButton alloc]init];
[rightButton setImage:[UIImage imageNamed:#"next-arrow.png"] forState:UIControlStateNormal];
[rightButton setFrame:CGRectMake(nameLabel.frame.origin.x+nameLabel.frame.size.width+4, 50, 14, 13)];
[rightButton addTarget:self action:#selector(next:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:nameLabel];
[view addSubview:imageView];
[view addSubview:leftButton];
[view addSubview:rightButton];
self.navigationItem.titleView = view;
This code is written in cellForRowAtIndexPath. The view is showing properly on navigation bar but after scrolling the tableview. Initially it is displaying on wrong coordinates.
Please Suggest.
Put this code in viewWillAppear or viewDidAppear. It's not meant to be in tableView protocol!
I am trying to set accessory view of cell which is UIButton. But it is not shown.
Here is the code
UIButton *accessory = [UIButton buttonWithType:UIButtonTypeCustom];
[accessory setImage:[UIImage imageNamed:#"plus.png"] forState:UIControlStateNormal];
[cell setAccessoryView:accessory];
Call [accessory sizeToFit]; to adjust its size according to the image.
You will need to set a frame to this accessory like this
UIButton *accessory = [UIButton buttonWithType:UIButtonTypeCustom];
[accessory setImage:[UIImage imageNamed:#"plus.png"] forState:UIControlStateNormal];
//Set the accessory frame
accessory.frame = CGRectMake(0, 0, 100, 100);
UIView* accessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 24, 50)];
UIImageView* accessoryViewImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"arrow.png"]];
accessoryViewImage.center = CGPointMake(12, 25);
[accessoryView addSubview:accessoryViewImage];
[cell setAccessoryView:accessoryView];
[accessoryViewImage release];
[accessoryView release];
just do like this.
When I created a UIView in my superview, I want to add a button to this view that act as an exit button.
So when pressed, it will remove this UIView from my superview, how should I do that?
The View and button in that view is created programmatically when a button is pressed
- (IBAction)skillButtonPressed:(UIButton *)sender
{
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(40, 130, 240, 232)];
UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[cancelButton setFrame:CGRectMake(0, 0, 32, 32)];
[cancelButton setTitle:#"x" forState:UIControlStateNormal];
[myView addSubview:cancelButton];
[self.view addSubview:myView];
}
In your class that creates the view, add a function like this:
//tag for subview - make it unique
#define SUBVIEW_TAG 9999
- (void) handleExit
{
UIView * subview = [self.view viewWithTag:SUBVIEW_TAG];
[subview removeFromSuperview];
}
and when creating the subview do:
- (IBAction)skillButtonPressed:(UIButton *)sender
{
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(40, 130, 240, 232)];
UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[cancelButton addTarget:self action:#selector(handleExit) forControlEvents:UIControlEventTouchUpInside];
[cancelButton setFrame:CGRectMake(0, 0, 32, 32)];
[cancelButton setTitle:#"x" forState:UIControlStateNormal];
[myView addSubview:cancelButton];
[myView setTag:SUBVIEW_TAG];
[self.view addSubview:myView];
//don't forget to release if you are not using ARC!
}
Here is the code for you:
self->myButton = [UIButton buttonWithType:UIButtonTypeCustom];
self-> myButton = CGRectMake(60, 6, 180, 30);
[self-> myButton setTitle:#"MyButton" forState:UIControlStateNormal];
[self-> myButton setTextColor:[UIColor whiteColor]];
self-> myButton = 1;
[self-> myButton addTarget:self action:#selector(myButtonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:self-> myButton];
Change the coordinates for your button placing
In myButtonTouchUpInside method implemetation,
[self removeFromSuperview];
Hope this is what you want..
You can even hide the view or [self removeFromSuperview];
I'm just wondering if anyone could help me change the position of a button label for a button that I've made with code.
Thanks a lot,
Chris
If you mean UIButton, look at UIButton's titleEdgeInsets property.
So the button label is an instance of UILabel, isn't it ?? Set the position of that label by setting the frame to it with respect to the UIButton frame.
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(3, 20, w, h)];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, w, h)];
[titleLabel setText:#"TITLE"];
[button addSubview:titleLabel];
[self.view addSubview:button];
[titleLabel release];
I hard coded the x and y positions of the button as well as for the label. You set some other value's and place the label in the respective position where ever you want.
If you want to set only the text position of that label you can do it like this:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(3, 20, w, h)];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, w, h)];
[titleLabel setText:#"TITLE"];
[titleLabel setTextAlignment:UITextAlignmentCenter];//(UITextAlignmentRight/UITextAlignmentLeft)
[button addSubview:titleLabel];
[self.view addSubview:button];
[titleLabel release];
Hope it will help you.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0,0,50,32);
[button setBackgroundImage:[UIImage imageNamed:#"btn_back.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(BackBtnClicked) forControlEvents:UIControlEventTouchUpInside];
UIView *backButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 32)];
backButtonView.bounds = CGRectOffset(backButtonView.bounds, -1, -4);
[backButtonView addSubview:button];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButtonView];
//barButtonItem.imageInsets = UIEdgeInsetsMake(-6, 20, 30, 0);
[self.navigationItem setLeftBarButtonItem:barButtonItem];
i'm extending UITableViewController and have a uibutton added programtticly to the table footer , for some reason the uibutton fire only when touch in the top part ... as if someting is masking the rest of it and i can't understand what it is
i have the following code at viewForFooterInSection function:
CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
UIView* footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenRect.size.width, 44.0)];
footerView.autoresizesSubviews = YES;
footerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
footerView.userInteractionEnabled = YES;
goButton = [UIButton buttonWithType:UIButtonTypeCustom];
[goButton addTarget:self action:#selector(getReport)forControlEvents:UIControlEventTouchUpInside];
[goButton setTitle:#"GO!" forState:UIControlStateNormal];
[goButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
goButton.frame = CGRectMake(50, 20, 220.0, 50.0);
UIImage *bg = [UIImage imageNamed: #"but_go.png"];
[goButton setBackgroundImage:bg forState:UIControlStateNormal];
[goButton setBackgroundColor:[UIColor clearColor]];
if (self.optimizations == nil || self.optimizations.count < 1){
goButton.enabled = NO ;
}
[footerView addSubview: goButton];
return footerView;
It's because the button's frame fell out of its parent view. You need to increase your footerView's frame size so the whole part of the button contains in the footerView. You can try to set footerView background color to blue or red to see the actual frame area.