iPad develop:customized button on navigationBar can't see after push->pop - ipad

iPad app:I've add some customized button to navigationBar as follows:
- (void)addEditButton{
UIButton *editButton = [UIButton buttonWithType:UIButtonTypeCustom];
editButton.frame = CGRectMake(0, 0, 50, 30);
[editButton setBackgroundImage:[[UIImage imageNamed:#"images_iPad.bundle/NavigationBar_Btn_iPad.png"] stretchableImageWithLeftCapWidth:15/2.0 topCapHeight:32/2.0] forState:UIControlStateNormal];
[editButton addTarget:self action:#selector(editAction:) forControlEvents:UIControlEventTouchUpInside];
[editButton setTitle:#"Edit" forState:UIControlStateNormal];
editButton.titleLabel.font = [UIFont fontWithName:kAPPBOLDFONT size:12];
editButton.titleLabel.shadowColor = [UIColor blackColor];
editButton.titleLabel.shadowOffset = CGSizeMake(0, -1);
UIBarButtonItem *editItem = [[[UIBarButtonItem alloc] initWithCustomView:editButton] autorelease];
self.navigationItem.rightBarButtonItem = editItem;
}
My problem is that:I clicked this button,it will push to another viewController, after popModelViewController:animate,I can't see this customized button, although this, it does work.and not just here ,others are the same,please help me.

Related

Title of a Custom UIBarButtonItem from a UIButton not showing

I made a custom UIBarButtonItem from a UIButton. The custom bar button is visible, the action works, the size is correct, and the background color can be seen; however the manually set title cannot be seen. maybe background is laid over the text? I'm not sure, help?
In viewDidLoad:
UIButton *resetButton = [UIButton buttonWithType:UIButtonTypeCustom];
resetButton.frame = CGRectMake(0, 0, 65, 30);
[resetButton addTarget:self action:#selector(speakPhrase:) forControlEvents:UIControlEventTouchUpInside];
resetButton.titleLabel.font = [UIFont fontWithName:#"ProximaNova-Bold" size:19];
resetButton.titleLabel.text = #"RESET";
resetButton.titleLabel.textColor = [UIColor whiteColor];
resetButton.backgroundColor = [UIColor redColor];
resetButton.showsTouchWhenHighlighted = YES;
UIBarButtonItem *resetBarBTN = [[UIBarButtonItem alloc] initWithCustomView:resetButton];
self.navigationItem.rightBarButtonItem = resetBarBTN;
You should set the title for button of using setTitle:forState: for particular control states, replace your code with below
UIButton *resetButton = [UIButton buttonWithType:UIButtonTypeCustom];
resetButton.frame = CGRectMake(0, 0, 65, 30);
[resetButton addTarget:self action:#selector(speakPhrase:) forControlEvents:UIControlEventTouchUpInside];
resetButton.titleLabel.font = [UIFont fontWithName:#"ProximaNova-Bold" size:19];
[resetButton setTitle:#"RESET" forState:UIControlStateNormal]; //change this line in your code
resetButton.titleLabel.textColor = [UIColor whiteColor];
resetButton.backgroundColor = [UIColor redColor];
resetButton.showsTouchWhenHighlighted = YES;
UIBarButtonItem *resetBarBTN = [[UIBarButtonItem alloc] initWithCustomView:resetButton];
self.navigationItem.rightBarButtonItem = resetBarBTN;
Use setTitle:forState to set button title. For example
[resetButton setTitle:#"text" forState:UIControlStateNormal]

Adding two UIBarButtomItems with image programmatically

I am trying to use following code in my viewDidLoad method to add two UIBarButtonItems to the left side of the navigation bar, but only the second button is shown:
//nav bar buttons
UIView* leftButtonView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
UIButton* leftButton = [UIButton buttonWithType:UIButtonTypeSystem];
leftButton.backgroundColor = [UIColor clearColor];
leftButton.frame = leftButtonView.frame;
[leftButton setImage:[UIImage imageNamed:#"toTop"] forState:UIControlStateNormal];
[leftButton setTitle:#"" forState:UIControlStateNormal];
leftButton.tintColor = [UIColor redColor]; //Your desired color.
leftButton.autoresizesSubviews = YES;
leftButton.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin;
[leftButton addTarget:self action:#selector(toTop:) forControlEvents:UIControlEventTouchUpInside];
UIView* leftButtonView2 = [[UIView alloc]initWithFrame:CGRectMake(85, 0, 40, 40)];
UIButton* leftButton2 = [UIButton buttonWithType:UIButtonTypeSystem];
leftButton2.backgroundColor = [UIColor clearColor];
leftButton2.frame = leftButtonView.frame;
[leftButton2 setImage:[UIImage imageNamed:#"toSectionTop"] forState:UIControlStateNormal];
[leftButton2 setTitle:#"" forState:UIControlStateNormal];
leftButton2.tintColor = [UIColor redColor]; //Your desired color.
leftButton2.autoresizesSubviews = YES;
leftButton2.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin;
[leftButton2 addTarget:self action:#selector(toTop:) forControlEvents:UIControlEventTouchUpInside];
[leftButtonView addSubview:leftButton];
[leftButtonView2 addSubview:leftButton2];
UIBarButtonItem* leftBarButton = [[UIBarButtonItem alloc]initWithCustomView:leftButtonView];
UIBarButtonItem* leftBarButton2 = [[UIBarButtonItem alloc]initWithCustomView:leftButtonView2];
self.navigationItem.leftBarButtonItem = leftBarButton;
self.navigationItem.leftBarButtonItem = leftBarButton2;
Any help is welcome. Thank you
You're setting the same property twice so the second setting replaces the first. Try:
self.navigationItem.leftBarButtonItems = #[ leftBarButton, leftBarButton2 ];
Note this is setting leftBarButtonItem*s*, which takes an array of bar buttons.
Also, you shouldn't need the container views, they add no value. Buttons are views so you can use them directly. Setting the frame origin of the view may also cause you issues.

Add a back arrow to leftBarButtonItem?

I want to add a back arrow to a leftBarButtonItem, to make it appear visually as if it was a regular back button, although it's functionality is slightly different.
Is there a way to do this?
You can use custom button and hide the back button if you are using navigation controller.
To hide back button use this:
self.navigationItem.hidesBackButton = YES;
And for Custom Button:
UIButton * customButton = [UIButton buttonWithType:UIButtonTypeCustom];
[customButton setBackgroundColor:[UIColor colorWithRed:197.0/255.0 green:190.0/255.0 blue:157.0/255.0 alpha:1.0]];
[customButton setTitle:#"Do Something" forState:UIControlStateNormal];
customButton.titleLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:11.0f];
[customButton.layer setCornerRadius:3.0f];
[customButton.layer setMasksToBounds:YES];
[customButton.layer setBorderWidth:1.0f];
[customButton.layer setBorderColor: [[UIColor grayColor] CGColor]];
customButton.frame=CGRectMake(0.0, 100.0, 50.0, 25.0);
[customButton addTarget:self action:#selector(customMethod:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem * customItem = [[UIBarButtonItem alloc] initWithCustomView:customButton];
customItem.tintColor=[UIColor blackColor];
self.navigationItem.leftBarButtonItem = customItem;
This will work for you.
Happy Coding
If you are using UINavigationController and then push new viewcontroller it will automatically show back button.
If you are Not Using UINavigationController than
Use Custom UIButton for that with back image
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(0, 0, 48, 37);
[backButton addTarget:self action:#selector(backButtonTapped) forControlEvents:UIControlEventTouchUpInside];
backButton.showsTouchWhenHighlighted = YES;
UIImage *backButtonImage = [UIImage imageNamed:#"back-button.png"];
[backButton setImage:backButtonImage forState:UIControlStateNormal];
backButton.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backBarButtonItem;
You can change the value for the frame and the imageEdgeInsets as per your requirements.
Refer this SO Answer.

How to change Bar Button Item colour?

In my detail view controller (part of navigation controller application) I've added custom "Back" button, like this:
- (void)loadView
{
[super loadView];
UIButton *backButton = [[UIButton alloc] initWithFrame: CGRectMake(0, 0, 10.0f, 24.0f)];
UIImage *backImage = [UIImage imageNamed:#"left_arrow_icon"];
[backButton setBackgroundImage:backImage forState:UIControlStateNormal];
[backButton setTitle:#"" forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(popBack) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backButtonItem;
}
-(void) popBack {
[self.navigationController popViewControllerAnimated:YES];
}
As you can see I added left bar button and assign "popBack" action. Basically left_arrow_icon is silver but when I press it, iOS change it darker grey.
My question is, can I (and how) change initial colour to white? Is that possible?
Edit. I use xcode5
Edit2:
That does't work too
Try this code. It works perfectly for me -
UIImageView *navigationBarImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Navigation.png"]];
navigationBarImage.frame = CGRectMake(0, 0, 320, 44);
navigationBarImage.userInteractionEnabled = YES;
navigationBarImage.backgroundColor = [UIColor colorWithRed:28.0/255.0 green:53.0/255.0 blue:102.0/255.0 alpha:1.0];
[self.view navigationBarImage];
[navigationBarImage release];
UIButton *backBarBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
backBarBtn1.frame = CGRectMake(13, 12, 20, 20);
[backBarBtn1 setImage:[UIImage imageNamed:#"ic_back3.png"] forState:UIControlStateNormal];
backBarBtn1.backgroundColor = [UIColor clearColor];
[backBarBtn1 addTarget:self action:#selector(backBtnAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:backBarBtn1];
Try this code -
[backbutton setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor], UITextAttributeTextColor,
[UIColor blackColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, -1)], UITextAttributeTextShadowOffset,
[UIFont fontWithName:HELVETICA_REGULAR_FONT size:17.0], UITextAttributeFont, nil] forState:UIControlStateNormal];

Adding custom back button on navigation bar

I am trying to add custom back button on navigation bar but it is not working below is my code
-(void)addLeftButton
{
UIImage *buttonImage = [UIImage imageNamed:#"btn_back.png"];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
aButton.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];
[aButton addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.backBarButtonItem = aBarButtonItem;
}
Please tell what's wrong with this code
Try this.
-(void)addLeftButton
{
UIImage *buttonImage = [UIImage imageNamed:#"btn_back.png"];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
aButton.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];
[aButton addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
[self.navigationItem setLeftBarButtonItem:aBarButtonItem];
}
Here is the code that i have used to create custom backbutton
- (void)viewDidLoad
{
[super viewDidLoad];
// Set the custom back button
//add the resizable image
UIImage *buttonImage = [[UIImage imageNamed:#"backbtn.png"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 13, 0, 5)];
//create the button and assign the image
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
MSLabel *lbl = [[MSLabel alloc] initWithFrame:CGRectMake(12, 4, 65, 28)];
lbl.text = #"Settings";
lbl.backgroundColor = [UIColor clearColor];
//lbl.font = [UIFont fontWithName:#"Helvetica" size:12.0];
lbl.numberOfLines = 0;
lbl.lineHeight = 12;
lbl.verticalAlignment = MSLabelVerticalAlignmentMiddle;
lbl.font = [UIFont fontWithName:#"Helvetica" size:12];
[button addSubview:lbl];
//set the frame of the button to the size of the image (see note below)
button.frame = CGRectMake(0, 0, 70, buttonImage.size.height);
[button addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
//create a UIBarButtonItem with the button as a custom view
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customBarItem;
}
-(void)back {
// Tell the controller to go back
[self.navigationController popViewControllerAnimated:YES];
}
This code will resize your image so it can fit the text.
You can also put this button in any method.
You didn't add action to your button. So you need to add action to your button. For more info see in the below code..
-(void)addLeftButton
{
UIImage *buttonImage = [UIImage imageNamed:#"btn_back.png"];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
aButton.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];
[aButton addTarget:self action:#selector(backBtnPressed) forControlEvents:UIControlEventTouchUpInside]; // -------- Edit here -----
self.navigationItem.backBarButtonItem = aBarButtonItem;
}
-(void)backBtnPressed
{
[self.navigationController popViewControllerAnimated:YES];
}

Resources