UIToolbar background colour is not changing - ios

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];

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];

How to add two toolbars over keyboard

I'm able to add a single toolbar over keyboard but i don't know how to add two consecutive toolbars over keyboard for single textView.
I'm able to add one tool bar just like below
But i want to do just like the below attachment.
There is no built in way of doing this. You will need to roll your own UIView / UIToolbar and place it above the keyboards first UIToolBar - you can use the system notifications to listen to UIKeyboard events and the adjust the second UIToolbar's frame property accordingly.
You can use UIToolbar and customize it to add your buttons into it. You can add buttons like this.
-(void)addToolBarOnKeyBordOnTextField:(UITextView *)textview
{
if (!viewToolbar) {
viewToolbar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 110)];
[viewToolbar setBackgroundColor:[UIColor clearColor]];
UIToolbar * numberToolbar1 = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0,self.view.frame.size.width, 50)];
numberToolbar1.barStyle = UIBarStyleBlack;
numberToolbar1.translucent = YES;
UIButton *buttonTopLeft = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 50)];
[buttonTopLeft setTitle:#"Clear" forState:UIControlStateNormal];
[numberToolbar1 addSubview:buttonTopLeft];
UIButton *buttonTopRight = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 60, 0, 60, 50)];
[buttonTopRight setTitle:#"Done" forState:UIControlStateNormal];
[numberToolbar1 addSubview:buttonTopRight];
[viewToolbar addSubview:numberToolbar1];
UIToolbar *numberToolbar2 = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 60,self.view.frame.size.width, 50)];
numberToolbar2.barStyle = UIBarStyleBlack;
numberToolbar2.translucent = YES;
UIButton *buttonBottomLeft = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 50)];
[buttonBottomLeft setTitle:#"Clear" forState:UIControlStateNormal];
[numberToolbar2 addSubview:buttonBottomLeft];
UIButton *buttonBottomRight = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 60, 0, 60, 50)];
[buttonBottomRight setTitle:#"Done" forState:UIControlStateNormal];
[numberToolbar2 addSubview:buttonBottomRight];
[viewToolbar addSubview:numberToolbar2];
}
[textview setInputAccessoryView:viewToolbar];
}
Remember to call this method in textViewShouldBeginEditing. You can add remaining buttons and set the frames as done in code.

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];

Add a sticky view underneath the Navigation Bar

I've seen a few other examples, but I haven't seen anything that actually sticks. What I want is essentially what a tableViewHeader does. I have a tableViewController with a navigation bar. I want to put a view into place that shows some search criteria in a small bar directly below the navbar. But I need it to stick when the user swipes to view the results.
I've tried adding a UIView as a subview to the navigation bar, but it always sits at the top of the Navigation Bar, overlaying everything.
Here's what worked:
// Create a custom navigation view
_navigationView = [[UIView alloc] init];
_navigationView.backgroundColor = [UIColor whiteColor];
_navigationView.frame = CGRectMake(0.0f,
self.navigationController.navigationBar.frame.origin.y + 44.0f,
self.view.frame.size.width,
30.0f);
// Create bottom border for the custom navigation view
_navigationViewBorder = [[UIView alloc] init];
_navigationViewBorder.backgroundColor = [UIColor darkGrayColor];
_navigationViewBorder.tag = 1;
_navigationViewBorder.frame = CGRectMake(0.0f,
self.navigationController.navigationBar.frame.origin.y + 74.0f,
self.view.frame.size.width,
0.5f);
// Add labels for date, results, and the time range
_dateDisplay = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 70, 15)];
[_dateDisplay setFont:[UIFont fontWithName:#"HelveticaNeue" size:13]];
[_dateDisplay setText:#""];
_timeRangeDisplay = [[UILabel alloc] initWithFrame:CGRectMake(98, 0, 135, 15)];
[_timeRangeDisplay setFont:[UIFont fontWithName:#"HelveticaNeue-Bold" size:13]];
[_timeRangeDisplay setText:#""];
_resultsDisplay = [[UILabel alloc] initWithFrame:CGRectMake(240, 0, 80, 15)];
[_resultsDisplay setFont:[UIFont fontWithName:#"HelveticaNeue" size:13]];
[_resultsDisplay setText:#""];
[_navigationView addSubview: _dateDisplay];
[_navigationView addSubview: _timeRangeDisplay];
[_navigationView addSubview: _resultsDisplay];
// Add two views to the navigation bar
[self.navigationController.navigationBar.superview insertSubview:_navigationView belowSubview:self.navigationController.navigationBar];
[self.navigationController.navigationBar.superview insertSubview:_navigationViewBorder belowSubview:_navigationView];
Why not add the view to your viewController's view, and set up its constraints so that it sits just below the navigationBar, but above the tableView?
If it's only supposed to be there some of the time, you can animate its constraints to show/hide it.

UIToolbar tintColor and barTintColor issues

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.

Resources