Custom UINavigationBar status bar not included background image - swift

I'm trying set background image for UINavigationBar but status bar always white color
Result
My code
self.navigationController?.navigationBar.setBackgroundImage(UIImage(named:"bg_nav")?.resizableImage(withCapInsets: UIEdgeInsets.zero, resizingMode: .stretch), for: .default)
Expected result image background fill include status bar

Just integrate below function inside your viewController where your need to change the status bar appearance.
func setStatusBarBackgroundColor(color: UIColor) {
guard let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView else { return }
statusBar.backgroundColor = color
}
uses: self.setStatusBarBackgroundColor(color: yourColor)
You can apply this to the whole app using common viewController and make this viewController to the sub class of other viewController's, by usign this approach you dont need to write this code again.

You can Do like that
extension UINavigationController{
func setNavBarImage(_ image:UIImage?) {
guard let image = image else {return}
self.navigationBar.setBackgroundImage(image, for: .default)
self.navigationBar.shadowImage = UIImage()
self.navigationBar.isTranslucent = true
//clear statusBar color
let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView
statusBar?.backgroundColor = UIColor.clear
}
}
And in ViewController used like that
self.navigationController?.setNavBarImage(UIImage(named: "NavigationBar"))
Result

Related

How to make UITabBar blurry, in Swift

I am trying to make UITabBar look blur.
I am trying to make something like this in this image
But my view now looks like this
This is my view for tabbar
I tried this code in UITabbarController -
Code:
class TabBarViewController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
configureTabbar()
}
func configureTabbar(){
let blurEffect = UIBlurEffect(style: .dark)
let vibrancyEffect = UIVibrancyEffect(blurEffect: blurEffect)
let vibrancyView = UIVisualEffectView()
vibrancyView.frame = tabBar.bounds
vibrancyView.autoresizingMask = .flexibleWidth
vibrancyView.effect = vibrancyEffect
tabBar.insertSubview(vibrancyView, at: 0)
tabBar.isTranslucent = true
tabBar.backgroundImage = UIImage()
tabBar.backgroundColor = .clear
tabBar.barStyle = UIBarStyle.black
tabBar.barTintColor = UIColor.clear
}
System bars such as UINavigationBar, UITabBar & UIToolbar are translucent by default and you don't need to add anything extra to get that effect.
You just need to make sure that your view extends it's content under system bars. You can go to storyboard and make sure the Extend Edges - Under Bottom Bars is checked for your UIViewController that you plan to see this effect on.

How can I animate the change of the navigation bar title

I am looking for a way to run a custom CAAnimation on the UINavigationBar title.
More precisely, I am looking for a way to access the label which displays navigationItem.title and run animations on that.
It is certainly possible to manually create a UILabel and set the navigationBar.titleView accordingly.
This however seems to be too much effort for a hopefully simple problem. Plus, it will not work well with large titles on the UInavigationBar.
The title text is accessible as topItem.text. There is no way to directly access the label which is displaying this text.
So if you want to animate this label, you first have to search for it in the subview of the NavigationBar.
Then, you can apply animations on this label.
See below for an example that fades in the new title from the right.
/// Fades in the new title from the right
///
/// - Parameter newTitle: New title to display on the navigation item
func animateTitle(newTitle: String) {
// Title animation code
let titleAnimation = CATransition()
titleAnimation.duration = 0.25
titleAnimation.type = CATransitionType.push
titleAnimation.subtype = CATransitionSubtype.fromRight
titleAnimation.timingFunction = CAMediaTimingFunction.init(name: CAMediaTimingFunctionName.easeInEaseOut)
// Find the Label which contains the topitem title
if let subviews = navigationController?.navigationBar.subviews {
for navigationItem in subviews {
for itemSubView in navigationItem.subviews {
if let largeLabel = itemSubView as? UILabel {
largeLabel.layer.add(titleAnimation, forKey: "changeTitle")
}
}
}
}
navigationItem.title = newTitle
}

How do I actually create a custom UISearchBar?

I have been banging my head against a wall for a day now trying to figure out how to create a custom UISearchBar so I can get rid of the stock black search bar UIKit thinks I want. I've tried a few 'solutions':
1. Customizing the appearance in the AppDelegate
//default background color for search bars
UISearchBar.appearance(whenContainedInInstancesOf: [UISearchController.self]).tintColor = UIColor(hexString: "#394C53")
This didn't work
2. Subclassing UISearchController and UISearchBar
I followed this tutorial which didn't work
3. Looping through subviews
let results = DestinationSearchResultsTableViewController()
results.mapView = map
results.handleMapSearchDelegate = self
destinationSearch = UISearchController(searchResultsController: results)
destinationSearch!.searchBar.placeholder = "Where do you want to go?"
if #available(iOS 11, *) {
navigationItem.searchController = destinationSearch
if let textfield = destinationSearch!.searchBar.value(forKey: "searchField") as? UITextField {
if let backgroundview = textfield.subviews.first {
// Below is not what I want but it works
backgroundview.backgroundColor = UIColor.white
backgroundview.layer.cornerRadius = 10
backgroundview.clipsToBounds = true;
}
}
} else {
self.navigationController?.pushViewController(destinationSearch!, animated: true)
}
destinationSearch!.delegate = self
destinationSearch!.searchResultsUpdater = results as UISearchResultsUpdating
This actually kinda works. It works really well if I use the UIColor predefined colors like UIColor.white UIColor.red etc. but it works really terribly if I want to use a specific color like this: I tried using RGB and hex representations of the color above, but all I get is black.
Here is what I want:
I want to create my own UISearchBar with a left-aligned label and content textfield to the right as I have shown above.
Thanks

setting custom icons in tabbar for highlighted/not highlighted

I have icons in my assets that I want to use for my tab bar, 2 for each tab (one representing not highlighted i.e just an outline, and one representing highlighted i.e all filled in). The idea is to use the outlined icons for the tabs that aren't currently selected, and the filled in one for the tab that is currently selected. How can I go about doing this?
Just make an Class for your Tabbar, for example:
class MainTabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
for item in self.tabBar.items! as [UITabBarItem] {
// loop through all of your elements in TabBar
if let image = item.image {
item.selectedImage = your Selected Image
item.image = your base Image
}
}
}
You could store all your images in an Array containing UIImage, like:
let TabImages = [UIImage]()
let HighlightedImages = [UIImage]()
And then set them in your for each loop.
var tabBarController = self.window!.rootViewController as UITabBarController
let tabItems = tabBarController.tabBar.items as [UITabBarItem]
tabItems[2].selectedImage = UIImage(named: "tabImage1_Selected")

Non Standart UITabBarItem Action

I have UITabBarController with view
So on every TabBar I have ViewController. But on my center UITabBarItem I need to call something like modal UIViewController. And It should be like this
My UITabBarController class look like
class PlanetTabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
self.tabBar.tintColor = kTintColor
var items = self.tabBar.items as! [UITabBarItem]
let centredTabBar:UITabBarItem = items[2]
self.tabBar.layer.borderWidth = 0.50
centredTabBar.image = ktabCentredBarImage
self.tabBar.layer.borderColor = UIColor.clearColor().CGColor
self.tabBar.layer.borderWidth = 0
self.tabBar.shadowImage = UIImage()
// self.tabBar.backgroundImage = ktabBarImage
self.tabBar.backgroundImage?.imageWithAlignmentRectInsets
print( UIDevice.currentDevice().modelName)
if( UIDevice.currentDevice().modelName != "iPhone 6" && UIDevice.currentDevice().modelName != "iPhone 6 Plus") {
self.tabBar.backgroundImage = ktabBarImage
}
if( UIDevice.currentDevice().modelName == "iPad 2" ) {
self.tabBar.backgroundImage = ktabBarImage
}
if let font = UIFont(name: "Avenir-Black", size: 10) {
let appearance = UITabBarItem.appearance()
let attributes = [NSFontAttributeName:font]
appearance.setTitleTextAttributes(attributes, forState: UIControlState.Normal)
}
}
}
I really have no idea how to override standart calling of UIViewController and call modal like this =) Please give me advice where I need to search
You can either place a UIButton in the same area as the UITabBarItem and fire an action or you can simply fire an action when the selectedSegmentIndex changes("value changed" in IB) for the UITabBar after creating a center UITabBarItem. The design choice is yours. The first step is to understand the objects you're working with. It would probably be easier to just add a button, center aligned, over top of the standard UITabBar at the bottom so you don't deal with a custom UITabBarController to handle that non-uniform image above the tab bar.
Or just create your own TabBar down there. It's very simply to setup a few UIButtons or UIViews next to one another that don't have a static height such as a UITabBarController which would allow for that center object.
UITabBarController
UIViewController
Presenting a ViewController