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];
Related
I am trying to develop an app with a navigation bar of custom size. (100px) In the navigation bar, I also want to include a back button. I have added both of them using the child view, but the back button is not working:
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *backBtnImage = [UIImage imageNamed:#"menueButton.png"];
UIImage *backBtnImagePressed = [UIImage imageNamed:#"menueButton.png"];
backBtn.exclusiveTouch = YES;
[backBtn addTarget:self action:#selector(back:) forControlEvents:UIControlEventTouchUpInside];
[backBtn setBackgroundImage:backBtnImage forState:UIControlStateNormal];
[backBtn setBackgroundImage:backBtnImagePressed forState:UIControlStateHighlighted];
backBtn.frame = CGRectMake(0, -40, 35, 32);
UIView *backButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, -40, 35 , 32)];
[backButtonView addSubview:backBtn];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:backButtonView];
self.navigationItem.leftBarButtonItem = backButton;
My navigation bar size is 100 pixels tall, and when I use button position (0,0,35,32) it will work correctly. But in this case, the button is being displayed much lower than intended. What am I doing wrong?
self.btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
[self.btnBack setImage:[UIImage imageNamed:#"back_active.png"] forState:UIControlStateNormal];
[self.btnBack addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
self.btnBack.frame = CGRectMake(5, 9, 50, 30);
[self.navigationController.navigationBar addSubview:self.btnBack];
check this link also.
Update:for back button
-(void)back
{
[self.btnBack removeFromSuperview];
[self.navigationController popViewControllerAnimated animated:NO];
}
Try allocing backButtonView like this
UIView *backButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 35 , 32)];
Change : orgin of view to (0,0)
Edit
Try
backBtn.frame = CGRectMake(0, 0, 35, 32);
UIView *backButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, -40, 35 , 32)];
I have a UINavigationController subclass and in the viewDidLoad I had the following code:
self.containerView_ = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, self.navigationController.navigationBar.frameHeight)];
[self.containerView_ setBackgroundColor:[UIColor yellowColor]];
[self.containerView_ setAutoresizingMask:UIViewAutoresizingNone];
self.profileButton_ = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[self.profileButton_ addTarget:self action:#selector(showProfileView:) forControlEvents:UIControlEventTouchUpInside];
[self.profileButton_ setCenterY:self.navigationController.navigationBar.frameHeight/2];
[self.profileButton_ setImage:[UIImage imageNamed:#"profile_icon.png"] forState:UIControlStateNormal];
self.socialButton_ = [[UIButton alloc] initWithFrame:CGRectMake(self.profileButton_.frameRight + 10, 0, 30, 30)];
[self.socialButton_ addTarget:self action:#selector(showSocialView:) forControlEvents:UIControlEventTouchUpInside];
[self.socialButton_ setCenterY:self.profileButton_.centerY];
[self.socialButton_ setImage:[UIImage imageNamed:#"social_icon.png"] forState:UIControlStateNormal];
self.notesButton_ = [[UIButton alloc] initWithFrame:CGRectMake(self.socialButton_.frameRight + 10, 0, 30, 30)];
[self.notesButton_ addTarget:self action:#selector(showNotesView:) forControlEvents:UIControlEventTouchUpInside];
[self.notesButton_ setCenterY:self.profileButton_.centerY];
[self.notesButton_ setImage:[UIImage imageNamed:#"notes_icon.png"] forState:UIControlStateNormal];
[self.containerView_ addSubview:self.profileButton_];
[self.containerView_ addSubview:self.notesButton_];
[self.containerView_ addSubview:self.socialButton_];
[self.navigationBar addSubview:self.containerView_];
I can see the UIButton but the container background is not even yellow color. When I press the button, the action of the UIButton is not called. Any idea why this is?
You are subclassing UINavigationController?
To quote Apple: "This class is not intended for subclassing."
The usual way to tweak the UINavController is to access the navBar from the perspecitve of a contained UIViewController. You would put the target/action methods in the UIViewController.
I have to create a UIBarButton on the having a globe and a text "Map" written on it.
What I have done:
I have added a UIButton with the image of the globe as backgroundImage.
Set the text on the button.
Then created a UIBarButton initWithCustomView from the above button.
Given it style as Bordered.
Result:
Now what i got is a plane button with the globe image stretched on that which is overlapping with the Text in it.
I have searched for this a lot but i only found links which show the image in background and the text above the button. Nothing like the image and text together.
Can anyone help me in creating it the way it is shown in the attachment.
Try this :
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:#"Title" forState:UIControlStateNormal];
UIImage *butImage_on = [[UIImage imageNamed:#"closebutton_on.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:10];
UIImage *butImage_over = [[UIImage imageNamed:#"closebutton_over.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:10];
[button setBackgroundImage:butImage_on forState:UIControlStateNormal];
[button setBackgroundImage:butImage_over forState:UIControlStateHighlighted];
[button setImage:[UIImage imageNamed:#"globe.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(closeProfilePage) forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake(0, 0, 61, 30);
[button setTitleEdgeInsets:UIEdgeInsetsMake(0, -15, 0, 2)];
[button setImageEdgeInsets:UIEdgeInsetsMake(30,
20,
0,
20)];
UIBarButtonItem *backButton = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
self.navigationItem.leftBarButtonItem = backButton;
Note: you need to manage insets according to your requirements.
Use image with #2x. if your graphices are high resolution. according to 640 X 960 px;:-
UIView *parentView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 6,51, 44)];
UIButton *btnBack = [[UIButton alloc] initWithFrame:CGRectMake(0, 6, 51, 30)];
[btnBack setBackgroundImage:[UIImage imageNamed:#"back#2x.png"] forState:UIControlStateNormal];
[btnBack addTarget:self action:#selector(backButtonClicked) forControlEvents:UIControlEventTouchUpInside];
[parentView1 addSubview:btnBack];
[btnBack release];
UIBarButtonItem *homeBarButtomItem2 = [[UIBarButtonItem alloc] initWithCustomView:parentView1];
[parentView1 release];
[self.navigationItem setLeftBarButtonItem:homeBarButtomItem2];
[homeBarButtomItem2 release];
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.
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 !!