UIToolbar tintColor and barTintColor issues - ios

I have code like so:
UIView *colorView = [[UIView alloc] init];
colorView.frame = CGRectMake(0.0, 0.0, self.view.frame.size.width, 64.0);
colorView.backgroundColor = [UIColor blackColor];
//colorView.tintColor = [UIColor blackColor];
UIToolbar *toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0.0, 0.0, self.view.frame.size.width, 44.0);
self.view addSubview:colorView];
[self.view addSubview:toolbar];
Why does the toolbar subview have a different color then my view? The view appears black and the toolbar appears light gray? Is there a blur or something causing this?

Behavior from some of the properties of UINavigationBar has changed from iOS 7. I have already explained this thing in my Answer.
Take a look at the Bar style for iOS 6 and iOS 7 :
You can note two points here :
You can change the Bar style to translucent dark instead of translucent light (default).
You can change the translucent property to NO from YES (default).

Try this code, it will help you,
UIToolbar *toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0.0, 0.0, self.view.frame.size.width, 44.0);
toolbar.barStyle = UIBarStyleBlackTranslucent;
toolbar.tintColor = [UIColor blackColor];
toolbar.alpha = 0.0;
Change the tintColor and alpha based on your requirement.

Related

Get the real color of the navigation bar

For my app on iPhone X I need to create a footer. No problem here.
BUT I need to put the same color as the navigation bar.
The color of the navigationBar is set on barTintColor and cannot be changed.
The app builded, a UIVisualEffect is set automatically on the navigationBar and the color change : it's lighter.
How can I get the color shown of the navigation bar?
What I tried but didn't work :
footer.backgroundColor = [UIColor myColor]
footer.tintColor = [UIColor myColor]
footer.tintColor = navigationBar.barTintColor
footer.backgroundColor = navigationBar.barTintColor
imageView.image = [self.navigationController.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault]; [footer addSubview: imageView];
UIVisualEffect *blurEffect;
blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
UIVisualEffectView* visualView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
[self.footer addSubview:visualView];
**The code for the navigation bar : **
self.navigationController.navigationBar.barTintColor = tintColor;
[self.navigationController.navigationBar setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont dpBoldFontWithSize:16.8],NSFontAttributeName,
fontColor,NSForegroundColorAttributeName,
nil]];
[self.navigationItem setTitle:title];
**The code for the footer **
self.footerView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight - 35, self.view.width, 35)];
[self.footerView setBackgroundColor:color];
[self.view addSubview:self.footerView];
Screenshot of the problem :
Screenshot
Or what about autoresizing mask ?
UIView * footerView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - 35, self.view.bounds.size.width, 35)];
footerView.backgroundColor = UIColor.redColor;
footerView.autoresizingMask = (UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth);
[self.view addSubview:footerView];

UIToolbar background colour is not changing

I am using the below code to display a toolbar as a accessory view for a picker view. I want to customise the tool bar colour. I am not sure whats wrong in this below code. Is there a better way to achieve this
UIToolbar* state_close = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
state_close.translucent =NO; //.barStyle = UIBarStyleDefault;
state_close.barTintColor = [UIColor colorWithRed:0.94 green:0.94 blue:0.94 alpha:1.0];
[state_close sizeToFit];
Thanks in Advance
Objective - C
Your code is working fine.
try to change y position of the state_close like below
UIToolbar *state_close = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 200, self.view.frame.size.width, 50)];
state_close.barTintColor = [UIColor blackColor];
state_close.alpha = 1.0;
[state_close sizeToFit];
[self.view addSubview:state_close];
if you want to set y = 0 then hide navigationBar.
[self.navigationController.navigationBar setHidden:true];

Color different in UISearchBar?

Following is the code to customize the searchBar embedded in the navigationBar. I set the same tintColor for both navigationBar and searchBar. Bur it seems there is some difference in tabbar's tint color and navigation bar tint color. What could be the problem?
- (void)viewdidLoad:(BOOL)animated
{
//Change the navigation bar color
[self.navigationController.navigationBar setBarTintColor:kAppNavigationItemGrayColor];
//Create a UISearchBar
UISearchBar *topBarSearchBar = [[UISearchBar alloc] init];
[topBarSearchBar setBarTintColor:kAppNavigationItemGrayColor];
//Wrapper view to hold searchbar
UIView *searchBarWrapper = [[UIView alloc]initWithFrame:CGRectMake(0.0, 0.0, 310.0, 44.0)];
[topBarSearchBar setFrame:CGRectMake(0.0, 0.0, 310.0, 44.0)];
[searchBarWrapper setBackgroundColor:[UIColor clearColor]];
[searchBarWrapper addSubview:self.topBarSearchBar];
[self.navigationItem setTitleView:searchBarWrapper];
self.navigationItem.hidesBackButton = YES;
}
Try this [searchBarWrapper setBackgroundColor: kAppNavigationItemGrayColor];
Try to set,
self.navigationController.navigationBar.translucent = NO;

when home button pressed view origin changed?

I have this code in viewDidLoad:
[self.navigationController setNavigationBarHidden:YES];
self.edgesForExtendedLayout = UIRectEdgeNone;
self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight;
self.view.backgroundColor = [UIColor whiteColor];
//we need to add white background behind the status bar
CGFloat statusBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height;
UIView* statusBarWhiteBackGround = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, statusBarHeight)];
[statusBarWhiteBackGround setBackgroundColor:[UIColor whiteColor]];
[self.view addSubview:statusBarWhiteBackGround];
UIView * redBackGroundColor = [[UIView alloc] init];
[redBackGroundColor setBackgroundColor:[UIColor redColor]];
redBackGroundColor.frame =CGRectMake(0,statusBarHeight , self.view.frame.size.width, self.view.frame.size.height-statusBarHeight);
[self.view addSubview:redBackGroundColor];
statusWhiteBackGround view is used to change the color of the status bar.
here is the result of the above code:
but when the app enter background and then back to foreground, the redBackGroundColor view change it's orging.y as you can see in the bellow picture:
what's the problem ?thanks
Create a UIView programatically on method
-(void)viewDidLayoutSubviews{
screen=[[UIScreen mainScreen] bounds];
//Simple view
self.customView = [[CustomAlertOKView alloc]initWithFrame:CGRectMake(0, 20, screen.size.width, screen.size.height)];
[self.view addSubView:customView];
}
or
Create a view using storyboard and set the x as 0, y value as 20,width and height based on the screen size
#IBOutlet UIView *customView;
set the outlet in the storyboard,
Then set the background color of the custom view to redcolor and main view to white color
self.view.backgroundColor = [UIColor whiteColor];
customView.backgroundColor = [UIColor redColor];

White line under UINavigationBar in IOS 7

I have a UITableViewController with a UISearchDisplayController and UISearchBar. I'm seeing a white line under the navbar when I present the view in a UITabBarController. When I present the view modally in a UINavigationController, the line is either gray or black (I can't tell) and it looks perfectly normal. Any ideas?
I had the same problem, couldn't figure out from where it did came from (it was present everywhere and it was NOT the shadowImage), ended up with the following fix (in my UINavigationController subclass)
// Fixes strange line under NavigationBar
{
UIView * view = [[UIView alloc] init];
view.backgroundColor = self.navigationBar.barTintColor;
CGRect rect = view.frame;
rect.origin.x = 0.f;
rect.origin.y = self.navigationBar.frame.size.height;
rect.size.width = self.navigationBar.frame.size.width;
rect.size.height = 1.f;
view.frame = rect;
[self.navigationBar addSubview:view];
}
I had the same problem too, after trying with a lot of methods,I find this way solved my problem
[[UISearchBar appearance] setBackgroundColor:[UIColor yourColor]];
write it in your viewDidLoad.
The white line is probably the shadowImage of navigation bar.
Try setting it as:
self.navigationController.navigationBar.shadowImage = [UIImage new];
Try setting the clipsToBounds property on the UISearchBar to YES.
Use following line of the code :
UIView *overlayView = [[UIView alloc] initWithFrame:CGRectMake(0, 43, 320, 1)];
[overlayView setBackgroundColor:[UIColor whiteColor]]; // set color accordingly
[navBar addSubview:overlayView]; // navBar is your UINavigationBar instance
[overlayView release];
here is my posted Answer :
Horizontal Separator NavBar IOS 7
How to remove UINavigatonItem's border line
Swift version of Divya's answers
let hideLineView = UIView(frame: CGRect(x: 0, y: navigationController!.navigationBar.frame.size.height, width: view.frame.size.width, height: 1))
hideLineView.backgroundColor = UIColor.white
navigationController!.navigationBar.addSubview(hideLineView)

Resources