it is impossible to hide status background when i'm using UITabbar in Swift - swift

I'm using UITab bar. In my UITabbar have 4 items and each of tabbar have navigation controller.
i want to put gradient color in the status background of view controller in 1-3 items but in view controller 4 i want it to have a status clear background.
my problem is when i go to tab 1-3 and then go to tab 4 the status background color will change according to the of tab 1-3. But I want the status background color of tab 4 is transparent.
the code that I use to change color of status background in tab 1-3
if let status = UIApplication.shared.value(forKey: "statusBar") as? UIView {
status.setGradientBackground(colorOne: Colors.carmine, colorTwo: Colors.cherryRed, colorThree: Colors.cherryRedTwo)
}
I have tried to hide status bar in Tab 4 by this method
1.change View controller-based status bar appearance to YES
2.put
setNeedsStatusBarAppearanceUpdate()
in viewdidload
3.put
override var prefersStatusBarHidden: Bool {
return true
}
in viewdidappear but still not work background of status bar is not hidding and the status background color still change according to the of tab 1-3.
it have one method that work by using this code
UIApplication.shared.isStatusBarHidden = true
and put it in every view controller
but it have warning like this
Setter for 'isStatusBarHidden' was deprecated in iOS 9.0: Use -[UIViewController prefersStatusBarHidden]
I want to know the correct way to change color of the status bar in UITab Thank you.

Ok, if I understand what you want correctly, you want to make your tab 4 to have a clear status bar, here's what you can try if your main view in tab 4, has subviews. Once you change your main view color, the status bar color changes also :
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
if let lastTab = tabBarController?.children[3] {
lastTab.view.backgroundColor = UIColor.white // status bar will be clear
}
}

Related

StatusBar text color doesn't change to Light Content [duplicate]

Adding
application.statusBarStyle = .lightContent
to my AppDelegate's didFinishLaunchingWithOptions method nor adding
override var preferredStatusBarStyle: UIStatusBarStyle {
return UIStatusBarStyle.lightContent
}
to the VC no longer works on iOS12/Xcode10
Any ideas?
This has nothing to do with iOS 12. You just have the rules wrong.
In a navigation controller situation, the color of the status bar is not determined by the view controller’s preferredStatusBarStyle.
It is determined, amazingly, by the navigation bar’s barStyle. To get light status bar text, say (in your view controller):
self.navigationController?.navigationBar.barStyle = .black
Hard to believe, but true. I got this info directly from Apple, years ago.
You can also perform this setting in the storyboard.
Example! Navigation bar's bar style is .default:
Navigation bar's bar style is .black:
NOTE for iOS 13 This still works in iOS 13 as long as you don't use large titles or UIBarAppearance. But basically you are supposed to stop doing this and let the status bar color be automatic with respect to the user's choice of light or dark mode.
If you choose a same status bar color for each View Controller:
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
Ad this to your Info.plist and set status bar color from Project -> Targets -> Status Bar Style by desired color.
On the other hand, in your case, you have a navigation controller which is embedded in a view controller. Therefore, you want to different status bar color for each page.
<key>UIViewControllerBasedStatusBarAppearance</key>
<true/>
Ad this to your Info.plist. Then, create a custom class for your NavigationController. After that you can implement the method:
class LightContentNavigationController: UINavigationController {
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
}
Thats it! Please, inform me whether this was useful!
If Matt's answer isn't working for you, try adding this line of code before you present your viewController.
viewController.modalPresentationCapturesStatusBarAppearance = true
I encountered a bug where setting modalPresentationStyle to overFullScreen does not give the status bar control to the presented view controller or navigation controller.
I was using navigation controller for each tab of UITabBarController. Subclassing UINavigationController and overriding childForStatusBarStyle fixed the issue for me.
class MyNavigationController: UINavigationController {
open override var childForStatusBarStyle: UIViewController? {
return topViewController?.childForStatusBarStyle ?? topViewController
}
}
If you have a modal UIViewController the situation becomes very tricky.
Short answer:
Present modal using UIModalPresentationStyle.fullScreen
override preferredStatusBarStyle (in your modal vc)
call setNeedsStatusBarAppearanceUpdate() in viewWillAppear (in your modal vc)
If you don't want to use UIModalPresentationStyle.fullScreen you have to set modalPresentationCapturesStatusBarAppearance
According to apple doc:
When you present a view controller by calling the
present(_:animated:completion:) method, status bar appearance
control is transferred from the presenting to the presented view
controller only if the presented controller's modalPresentationStyle
value is UIModalPresentationStyle.fullScreen. By setting this property
to true, you specify the presented view controller controls status bar
appearance, even though presented non-fullscreen.
The system ignores this property’s value for a view controller
presented fullscreen.
You can set
vc.modalPresentationCapturesStatusBarAppearance = true
to make the customization works.
Customizing UINavigationController can fix the issue
class ChangeableStatusBarNavigationController: UINavigationController {
override var preferredStatusBarStyle: UIStatusBarStyle {
return topViewController?.preferredStatusBarStyle ?? .default
}
}
Ref: https://serialcoder.dev/text-tutorials/ios-tutorials/change-status-bar-style-in-navigation-controller-based-apps/

Erase borders between navigation bar and searchBar swift 4

I am setting both the navigation bar and the search bar to a custom UIColor (which I call categoryColor in my code). When I do that, I still see an upper grayish line between nav bar and search bar. I have already set the searchBar border color to be the same as the others, but that gray line still exists. Does anyone know how to get rid of it?
Here is my code:
override func viewWillAppear(_ animated: Bool) {
//defining the color that will be used for all the items
let categoryColor = UIColor(hexString: selectCategory?.categoryColorHex ?? UIColor.randomFlat.hexValue())
//changing navigation bar tint color
navigationController?.navigationBar.barTintColor = categoryColor
//changing searchbar tint color
searchBar.barTintColor = categoryColor
//change searchBar border's color
searchBar.layer.borderColor = categoryColor?.cgColor
searchBar.layer.borderWidth = 3
//changing title that appears at the top, after list is loaded
title = selectCategory?.listName ?? "Todoey"
}
Here is a picture of what I see when I run the simulation:
The better way for implementing search bar with nav controller would be to use searchController inside a navigationController so the searchController will have the same background as navigationController. Here’s a great tutorial about that: https://m.youtube.com/watch?v=h7caxpp3Xps
Edit:
Also if you already implemented search capabilities you can do that with searchController too. Just set navigationController.searchConroller.searchBar.delegate for class that’s responsible for handling delegate methods

Swift 3 | Unable to hide status bar on a ViewController

I want to show the status bar on portrait mode only, when I rotate the screen the status bar hides as intended but if I push / show a new View Controller the status bar appears again.
I've tried the following without success:
Setting View controller-based status bar appearance to NO in the info.plist file
Using setNeedsStatusBarAppearanceUpdate() on self and on NavigationController
Setting prefersStatusBarHidden: Bool to true
Also tried UIApplication.shared.setStatusBarHidden(true, with: UIStatusBarAnimation.none)
Also extending NavigationController. seen here
Any help will be appreciated. Thanks.
Try Following Code
override var prefersStatusBarHidden: Bool {
if UIDevice.current.orientation.isPortrait{
return false
}
return true
}

UITabBarController customisation page top bar colour swift

How do I change the colour of the top bar of the customisation page of the more view controller. Please see the linked image. Sorry I can't post image here because of my low reputation.
Screenshot Image
Edited with more info:
I have managed to change the background color using the following code. But cant change the color of the top bar.
func tabBarController(tabBarController: UITabBarController, willBeginCustomizingViewControllers viewControllers: [AnyObject]) {
var editView : UIView = tabBarController.view.subviews[1] as! UIView
editView.backgroundColor = UIColor.blackColor()
}
Basically 2 Ways you can achieve this if you are using Global Unique colour in the app for All navigation bar
Use this Solution on App Launch:
UINavigationBar.appearance().barTintColor = UIColor.redColor()
Or if you want to change the Color of More navigationcontroller only then
Use this Solution by getting the Tabbar Reference :
self.tabBarController?.moreNavigationController.navigationBar.barTintColor = UIColor.greenColor()
You can use second solution in the First viewcontroller of the Tab, because that contains your tabbar reference

Change tintColor of Tab Bar in UIView via Storyboard or Swift?

I've found a post on how to change the tint color of the tab bar button, but it assumes you are using a tab bar controller. I tried any way and did not make a difference. I am using a regular UIView and dragged a Tab Bar control on there. How do I change the button tint color in this scenario? The storyboard and code suggestions are not making a change. I tried to add these into my viewDidLoad ever, but neither had an effect:
self.view.tintColor = UIColor.orangeColor()
self.tabBarController?.tabBar.tintColor = UIColor.orangeColor()
I was able to change the nav bar buttons tints via the storyboard no problem, but the tab bar isn't having any effect. I am trying to match the changes I did to the nav bar:
If you are using a Tab Bar Controller, this will work for the tab bar background color:
tabBarController?.tabBar.barTintColor = UIColor.whiteColor()
And this for the color of the items within the tab bar:
tabBarController?.tabBar.tintColor = UIColor.blackColor()
If you are not using a Tab Bar Controller, and you just dragged a tab bar into your view controller: Control drag from the tab bar in your storyboard to your view controller's swift file to create a new referencing outlet. It should something like this if you're unfamiliar:
#IBOutlet weak var myTabBar: UITabBar!
Then use that same lines of code above just replacing a few things:
myTabBar.barTintColor = UIColor.whiteColor()
myTabBar.tintColor = UIColor.blackColor()