Programmatically created UIButtonBarItem not picking up UIAppearance - ios

I've added UIAppearance code in my appdelegate to customize UIButtonBarItem.
The back buttons in a nav controller pick up the UIappearance.
But the UIButtonBarItems I create programmically to appear on the first navbar dont get the uiappearance.
Isn't UIAppearance supposed to apply to all UIButtonBarItems that are created?
The same thing happens if I put in a UIButtonBarItem into a navbar in storyboard.
The navbar picks up the UIAppearance but the UIButtonBarItem does not.
in appdelegate
-(void) configureAppearance
{
// navbar background
UIView* bk0 = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 480.0f, 44.0f)];
UIView* bk1 = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 480.0f, 22.0f)];
UIView* bk2 = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 22.0f, 480.0f, 22.0f)];
[bk0 addSubview:bk1];
[bk0 addSubview:bk2];
bk1.backgroundColor=LIGHT_PURPLE;
bk2.backgroundColor=DARK_PURPLE;
UIImage* navimg=ChangeViewToImage(bk0);
[[UINavigationBar appearance] setBackgroundImage:navimg forBarMetrics:UIBarMetricsDefault];
#define BUTTON_SIZE 34.0f
UIView* bbk0 = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, BUTTON_SIZE, BUTTON_SIZE)];
UIView* bbk1 = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, BUTTON_SIZE, BUTTON_SIZE)];
bbk0.backgroundColor=[UIColor clearColor];
bbk1.backgroundColor=DARK_PURPLE;
[bbk0 addSubview:bbk1];
addBorderAndShadow(bbk1, VERY_DARK_PURPLE, 1.0, 2.0);
UIImage* bimg=ChangeViewToImage(bbk0);
bimg = [bimg resizableImageWithCapInsets:UIEdgeInsetsMake(0.0f, BUTTON_SIZE, 0, 0)]; // this will keep it square
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:bimg
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, bimg.size.height*2) forBarMetrics:UIBarMetricsDefault]; // to not show text on button but still get nav ani
//[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setBackButtonBackgroundImage:bimg forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
}
in one of my viewcontrollers (which doesnt get uiappearance
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
//AppDelegate* appdel = (AppDelegate*)[[UIApplication sharedApplication] delegate];
//[appdel configureAppearance];
UIBarButtonItem* backButton = [[UIBarButtonItem alloc] initWithTitle:#"" style:UIBarButtonItemStylePlain target:self
action:#selector(revealMenu:)];
self.navigationItem.leftBarButtonItem = backButton;
}

Try like this:
[UIBarButtonItem apperanceWhenContainedIn: [UINavigationController class],nil]
setBackgroundImage:bimg
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];

Related

iOS - How to custom vertical position of navigation back bar item

I have a design of navigation bar like this. I've ready increased the height of Navigation Bar, but how can I set the position of Back button image and align left the title?
#import "UINavigationBar+Custom.h"
#implementation UINavigationBar (Custom)
- (CGSize)sizeThatFits:(CGSize)size {
CGRect rec = self.frame;
CGRect screenRect = [[UIScreen mainScreen] bounds];
rec.size.width = screenRect.size.width;
rec.size.height = 77;
return rec.size;
}
#end
NavBar picture design
Reality
Update: My code to add back bar button
- (void)viewDidLoad {
[super viewDidLoad];
[self configNavBar];
}
- (void)configNavBar {
// -- Config NavigationBar Background --
self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
self.navigationController.navigationBar.translucent = NO;
// -- Navigator title font --
NSDictionary *titleAttributes = #{
NSFontAttributeName :FONT_NAVIGATION_TITLE_BAR,
NSForegroundColorAttributeName : [Utils colorWithHexString:#"#0f385a"] };
[self.navigationController.navigationBar setTitleTextAttributes:titleAttributes];
// -- Back button color --
[self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
UIImage *myImage = [UIImage imageNamed:#"ic_arrow_back"]; //set your backbutton imagename
UIImage *backButtonImage = [myImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
// now use the new backButtomImage
[[UINavigationBar appearance] setBackIndicatorImage:backButtonImage];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:backButtonImage];
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(-0.f, -0)
forBarMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackgroundVerticalPositionAdjustment:40 forBarMetrics:UIBarMetricsDefault];
// -- Empty back title --
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"" style:UIBarButtonItemStylePlain target:nil action:nil];
[self.navigationItem setBackBarButtonItem:backButtonItem];
}
Try this codeUIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"image"] style:UIBarButtonItemStylePlain target:self action:#selector(action)];
[self.navigationItem setLeftBarButtonItem:barButtonItem];. And correctly format the image

Changing UINavigationBar later in View Controller

I have a view controller that does not have a NavigationController (and can't). I define a navigation bar like this.
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 20, [UIScreen mainScreen].bounds.size.width, 41)];
navBar.backgroundColor = UIColorFromRGB(0x72CE97);
navBar.barTintColor = UIColorFromRGB(0x72CE97);
//Keep the container for Navigation Items
UINavigationItem *navItem = [[UINavigationItem alloc] init];
UIImage* logoImage = [UIImage imageNamed:#"nav_logo_small.png"];
navItem.titleView = [[UIImageView alloc] initWithImage:logoImage];
//Add the left button as an Item to Navigation Items
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:self action:#selector(backButton:)];
navItem.leftBarButtonItem = leftButton;
//Add Navigation Items Container to Navigation Bar
navBar.items = #[ navItem ];
//Display the Navigation Bar
[self.view addSubview:navBar];
[navBar release];
How do I change the background later in the View Controller?
Below doesn't work. I think it is because I don't have a NavigationController:
NavigationBar *navigationBar = self.navigationController.navigationBar;
navigationBar.backgroundColor = [UIColor colorWithRed:197.0f/255.0f green:174.0f/255.0f blue:135.0f/255.0f alpha:1.0];
navigationBar.barTintColor = [UIColor colorWithRed:197.0f/255.0f green:174.0f/255.0f blue:135.0f/255.0f alpha:1.0];
Try this:
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 20, [UIScreen mainScreen].bounds.size.width, 41)];
navBar.tag = 1;
...
NavigationBar *navigationBar = (NavigationBar *)[self.view viewWithTag:1];

Setting the appearance of just one UIToolbar in iOS 8

I have two toolbars stacked on top of each other. They are created programmatically. The bottom bar uses standard UIButton elements with a solid colour background and the buttons inherit the tint.
I want the toolbar above it however to have a background image. I can only get this to work by using the appearance setter, however the appearance property is applied to both toolbars. Can anyone see a way around this?
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
///top toolbar///
UIToolbar *toolbar2 = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height-84, self.view.bounds.size.width, 44)];
toolbar2.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
[self.view addSubview:toolbar2];
UIImage *testimage = [[UIImage imageNamed:#"test.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UIToolbar appearance] setBackgroundImage:testimage forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
[[UIToolbar appearance] setBackgroundImage:testimage forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsLandscapePhone];
///////bottom toolbar////
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height-44, self.view.bounds.size.width, 44)];
toolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
toolbar.barTintColor=[UIColor colorWithHexValue:0xff336600];
[self.view addSubview:toolbar];
UIBarButtonItem *button1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"help.png"] style:UIBarButtonItemStylePlain target:self action:#selector(openHelpView)];
button1.tintColor=[UIColor whiteColor];
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *button2=[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"web.png"] style:UIBarButtonItemStylePlain target:self action:#selector(openWebView)];
button2.tintColor=[UIColor whiteColor];
UIBarButtonItem *button3 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"fb-icon.png"] style:UIBarButtonItemStylePlain target:self action:#selector(openFBView)];
button3.tintColor=[UIColor whiteColor];
UIBarButtonItem *button4 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"twitter.png"] style:UIBarButtonItemStylePlain target:self action:#selector(openTwitterView)];
button4.tintColor=[UIColor whiteColor];
[toolbar setItems:[[NSArray alloc] initWithObjects:button1,spacer,button3,spacer,button4,spacer,button2, nil]];
[self.view addSubview:toolbar];
Figured it out.
Just created an empty subclass of the UIToolbar class and then created my top Toolbar from that instead. That way I can apply the appearance property separately.

putting labels, buttons on the navigation bar iOS

I have created custom navigation controller,
I want to be added, a date at the left, a back button on the right and the title next to back button.
I tried to add one label, but it does not work. Please show me a way
UINavigationBar *naviBarObj = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 1024, 66)];
UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(20,8,280,30)];
navLabel.text = #"My Text";
[self.navigationController.navigationBar addSubview:navLabel];
[self.view addSubview:naviBarObj];
UINavigationBar *naviBarObj = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 1024, 66)];
UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(200,8,200,30)];
navLabel.text = #"My Text";
navLabel.textColor = [UIColor redColor];
[naviBarObj addSubview:navLabel];
[navLabel setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:naviBarObj];
try this it will work .It works for me :)
add custom view in UIToolbar
UIToolbar *tools = [[UIToolbar alloc]
initWithFrame:CGRectMake(0.0f, 0.0f,190.0f, 44.01f)];
tools.barStyle = -1; // clear background
NSMutableArray *buttons = [[NSMutableArray alloc] init];
UIBarButtonItem *bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
bi.width =10.0f;
[buttons addObject:bi];
[buttons addObject:add your view];
bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
bi.width =10.0f;
[buttons addObject:bi];
[buttons addObject:add your view];
[tools setItems:buttons animated:NO];
UIBarButtonItem *buttons_ = [[UIBarButtonItem alloc] initWithCustomView:tools];
self.navigationItem.rightBarButtonItem = buttons_; //same to leftBarButtonItem
You can also achieve this from Interface Builder
1. First add a UIView which will look like following
2. Then add a UILabel inside the UIView which will look like following
3. In the Center It will look like following
Now add necessary constraint to meet your requirement.
You need to add navigation item to the navigation bar. One per each navigation level. So you should have at least one of them.
** update **
UINavigationItem * myNavItem = [[UINavigationItem alloc] initWithTitle:#"Some title"];
/*
* add buttons and labels
*/
[self.navigationController.navigationBar pushNavigationItem:myNavItem animated:NO];
You need to change the titleView of the topItem. Like this:
naviBarObj.topItem.titleView = navLabel;
** update: Complete code (tested and working) **
UINavigationBar *naviBar = [[UINavigationBar alloc] initWithFrame: CGRectMake(.0, .0, 320.0, 44.0)];
UINavigationItem *navItem = [[UINavigationItem alloc] init];
navItem.titleView = titleLbl;
[naviBar pushNavigationItem: navItem animated: NO];
[navItem release]; // you don't need this for ARC
[self.view addSubview: naviBar];
[naviBar release]; // you don't need this for ARC

UIBarButton not showing in UINavigationBar

I need help with my UIBarButtonItem not showing in my UINavigationBar.
I am trying to put a UINavigationBar in my third view. As I am not quite sure if UINavigationController will work, I decided to manually initialized a UINavigationBar, together with its UINavigationItem initwithTitle. The code works, but my problem is adding a UIBarButtonItem because it won't show in the UINavigationBar.
- (void)viewDidLoad
{
[super viewDidLoad];
UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
UINavigationItem *navigationItem = [[UINavigationItem alloc] initWithTitle:#"Title"];
[navigationBar pushNavigationItem:navigationItem animated:NO];
UIBarButtonItem *button = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:#selector(showPI:)];
self.navigationItem.rightBarButtonItem = button;
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0, 44.0, self.view.frame.size.width, self.view.frame.size.height - navigationBar.frame.size.height) style:UITableViewStylePlain];
self.tableView = tableView;
[self.view addSubview:tableView];
[self.view addSubview:navigationBar];
[self showCurrencies];
self.tableView.dataSource = self;
self.tableView.delegate = self;
}
I'm not sure what's missing.
In your case, you're creating your own navigationBar. Try not to alloc init your navigationItem, instead use
UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
UINavigationItem *item = [navigationBar.items lastObject];
Then set your created barbuttonItem to the item's rightBarButtonItem,
item.rightBarButtonItem = button;
It's works for me.

Resources