Setting background image for a non-translucent navigation bar swift 4 - ios

I wanna change the background color of my navbar, while set the translucency to false, It's just plain white and it won't change untill I set an image for the background color. any help would be appreciated.
let homeViewController = HomeViewController();
let navigationController = UINavigationController(rootViewController: homeViewController);
let navBar = navigationController.navigationBar;
navBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor:UIColor.white];
navBar.setBackgroundImage(UIImage(), for: .default);
navBar.shadowImage = UIImage();
navBar.isTranslucent = false;
navBar.backgroundColor = UIColor.red; //not working
navBar.tintColor = UIColor.red //not working
* EDIT *
Thank you so much you guys, yea that was it, can you please help me a bit further. the reason I wanted to change the color of navbar was because I wanted to make it transparent so I could add a gradient background to it, and with barTintColor now I could set it to clear.
but still this
navBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor:UIColor.white];
navBar.setBackgroundImage(UIImage(), for: .default);
navBar.shadowImage = UIImage();
navBar.isTranslucent = false;
navBar.barTintColor = .clear;
doesn't work and I still don't see my gradient which I set like this :
let bg = UIImageView(image: UIImage(named: "gradient"));
bg.frame = CGRect(x: 0, y:0, width: UIApplication.shared.statusBarFrame.width, height: UIApplication.shared.statusBarFrame.height + (navBar.frame.height));
bg.contentMode = .scaleAspectFill;
bg.layer.masksToBounds = true;
bg.layer.opacity = 1;
navigationController.view.insertSubview(bg, belowSubview: navBar);
The reason I'm not just setting the background image with setBackgroundImage is I wanna change the position of the background image in the navbar so it should be on the right of the navbar.
I know I got the answer to my question but this was the real one and still can't figure it out. I'm gonna mark the best answer anyway either if you guys answer me or not but I would really appreciate if you could help me further.
EDIT *
Added Image

You set the wrong property.
Set barTintColor to apply your background color:
navBar.barTintColor = UIColor.red
Here's the Apple docs: barTintColor

This property is for Bar Text Color.
navBar.tintColor
Use This: (for NavBar Background Color).
navBar.barTintColor

Related

Custom Tab Bar Background Image is not showing correctly

I'm implementing a custom Tab bar for my iOS app. Im using the following code to display a background image:
class TabNavigationMenu: UIView {
// ...
// ...
UIGraphicsBeginImageContext(self.frame.size)
UIImage(named: "tabBarbg.png")?.draw(in: self.bounds)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
if let image = image {
self.backgroundColor = UIColor(patternImage: image)
}
}
However, the tab bar is presented like this:
Also, when i make a view and present it anywhere else in the screen, it displays correctly. I'm using the same code there as well. Heres an example:
Any idea what the problem could be? I'm guessing the solution would also solve the faint blue line on top of the view...
You can try this to customize tab bar in ios
//Set the background color
UITabBar.appearance().backgroundColor = .red
tabBar.backgroundImage = UIImage(named: "tabBarbg.png")
//Set the item tint colors
tabBar.tintColor = .white
tabBar.unselectedItemTintColor = .lightGray

My navigation bar is not moving up when scrolling UITableView with background image

I have this view hierarchy on a view embedded in a UINavigationController:
When I scroll the UITableView the navigation bar is not moving up (the title is not becoming smaller) it stays like this:
If I remove the image view as background view everything works well.
My navigation is configured like this:
navigationItem.title = "Title here"
navigationItem.largeTitleDisplayMode = .always
navigationController?.navigationBar.prefersLargeTitles = true
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.isTranslucent = true
navigationController?.navigationBar.tintColor = .white
navigationController?.navigationBar.barStyle = UIBarStyle.blackTranslucent
navigationController?.navigationBar.backgroundColor = .clear
A project demonstrating the problem is available here:
https://drive.google.com/file/d/181Aggala2ZfGN0lDjEtHWg0vobkM0iJc/view?usp=sharing
I already tried to change the insets of the tableview but it didn't work.
tableView.contentInset = UIEdgeInsets(top: navigationController?.navigationBar.height, left: 0, bottom: 0, right: 0)
Thanks!
As you discovered, to make large title fonts work as you want them to, the UIScrollView or any of its subclass needs to be the first element in the view hierarchy.
To fix your problem, you can try setting the background image to be the background of the UITableView directly.
Edit: Okay soo according to your comment you want a background behind everything including navigation bar. There is one way of achieving this and that is to subclass your UINavigationController and inside viewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
let image = UIImage(named: "wallpaper")
let imageView = UIImageView(image: image)
imageView.contentMode = .scaleAspectFill
imageView.frame = UIScreen.main.bounds
//this below part is important. Make sure to set it to 0 otherwise it will cover everything else
view.insertSubview(imageView, at: 0)
}
And then make sure your UIViewController containing the UITableView has a clear color for the UIView and remove the background image from that UIViewController

TabBar icon becoming a rectangle when its state is selected

I use two icons for different states tabBarItem.
My problem is that when tabbar is selected one icon to become a rectangle.
I did the other icons, and they appear well. I was looking for any information not found on this topic. How can I fix it?
My code
override func viewDidLoad() {
super.viewDidLoad()
let triviaMainTableViewController = StoryboardManager.triviaStoryboard.instantiateViewControllerWithIdentifier("TriviaMainTableViewController") as! TriviaMainTableViewController
viewControllers = [triviaMainTableViewController]
tabBarItem.image = UIImage(named: "TriviaTabBarDefault")?.imageWithRenderingMode(.AlwaysOriginal)
tabBarItem.selectedImage = UIImage(named: "TriviaTabBarSelected")
tabBarItem.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -3)
navigationBar.barTintColor = ColorManager.greenColor
}
You need to make sure you have put your icon on a transparent background in order for the selection highlight to work correctly. If the background color of the image isn't transparent it may look fine when it is not selected, but not when it is selected.

Black background on transparent UITabBar

I am trying to make a blurred background the UITabBar for my UITabViewController, and the idea is to have it be blurred and transparent so that the views underneath can be seen scrolling by.
Unfortunately I cannot for the life of me get the tab bar to be transparent. No matter what I do, there is always some black background to the tab bar that prevents the underlying view controllers from showing through.
If I change the alpha of the UITabBar to something low I can see that the tableview is indeed behind it, however you can see that the UITabBar has some sort of background to it that is preventing the tableview from fully showing through (and I don't want to bar button items to be invisible, just the tab bar background).
How can this be?
In the custom tab bar's view did load I have:
self.tabBar.translucent = true
self.tabBar.alpha = 0.3
self.tabBar.backgroundColor = UIColor.clearColor().colorWithAlphaComponent(0.0)
self.tabBar.layer.backgroundColor = UIColor.clearColor().colorWithAlphaComponent(0.0).CGColor
self.tabBar.backgroundImage = nil
self.tabBar.shadowImage = nil
and in the AppDelegate I have:
UITabBar.appearance().barTintColor = UIColor.clearColor()
UITabBar.appearance().tintColor = kColorAccent
UITabBar.appearance().translucent = true
UITabBar.appearance().translucent = true
UITabBar.appearance().backgroundColor = UIColor.clearColor()
UITabBar.appearance().backgroundImage = nil
UITabBar.appearance().layer.backgroundColor = UIColor.clearColor().CGColor
UITabBar.appearance().shadowImage = nil
...yeah It's excessive but I want to try everything.
Any ideas on what to do?
Make a UITabBar transparent
Assign a clear image to its backgroundImage. You can use a 1x1 clear.png, or create one programmatically:
self.backgroundImage = UIImage.imageWithColor(UIColor.clearColor())
This will make the UITabBar transparent:
Add a blur effect
Insert a UIVisualEffectView as the rearmost subview.
let frost = UIVisualEffectView(effect: UIBlurEffect(style: .Light))
frost.frame = self.bounds
self.insertSubview(frost, atIndex: 0)
This will insert a UIBlurEffect (frost):
Example
Set the Custom Class for the UITabBar of the Tab Bar Controller to FrostyTabBar.
You have a few options to supply a clearColor image. You can create a clear.png image with an alpha of 0. A programmatic elegant solution is described here.
If using a clear.png, assign it to the Background Image in the Attribute Inspector:
In Interface Builder, pick Style: Default & Translucent.
Once you take control of the background blur with a UIVisualEffectView, you can in turn supply any UIVisualEffect you so desire.
The entire FrostyTabBar class looks like this:
import UIKit
class FrostyTabBar: UITabBar {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
let frost = UIVisualEffectView(effect: UIBlurEffect(style: .light))
frost.frame = bounds
frost.autoresizingMask = .flexibleWidth
insertSubview(frost, at: 0)
}
}
► Find this solution on GitHub and additional details including a 1x1 clear.png on Swift Recipes.
I found a prefect solution, you only need to subclass UITabBar and then do the following actions to clean that annoying views
class MainTabBar: UITabBar {
var cleanDone = false
override func layoutSubviews() {
super.layoutSubviews()
self.deleteUnusedViews()
}
func deleteUnusedViews() {
if !self.cleanDone {
var removeCount = 0
for (_, eachView) in (self.subviews.enumerate()) {
if NSStringFromClass(eachView.classForCoder).rangeOfString("_UITabBarBackgroundView") != nil {
eachView.removeFromSuperview()
removeCount += 1
}
if NSStringFromClass(eachView.classForCoder).rangeOfString("UIImageView") != nil {
eachView.removeFromSuperview()
removeCount += 1
}
if removeCount == 2 {
self.cleanDone = true
break
}
}
}
}
}
the only solution that worked for me was this:
UITabBar.appearance().shadowImage = UIImage()
UITabBar.appearance().backgroundImage = UIImage()
and set: (you can do this in storyboard as well)
UITabBar.appearance().barTintColor = UIColor.clear
but what i have to set in storyboard is:
tabbar : translucent -> true

Transparent UINavigationBar without border

With iOS 7, it's now pretty easy to add a blur to UINavigationBar, even with a BarTint, see http://blog.ashleynh.me/frosted-uinavigationbar/ and this example image:
However, there's a border at the bottom. How can I get rid of the border to look more like this?
UPDATE:
I took Danny and Shali's code, and here are the results. As you can see, the border doesn't show any more but there is no blur.
let navigationBarAppearance = UINavigationBar.appearance()
navigationBarAppearance.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
navigationBarAppearance.shadowImage = UIImage()
navigationBarAppearance.translucent = true
and here's the Inspector screenshot:
I also tried:
let navigationBarAppearance = UINavigationBar.appearance()
let clearImage = UIImage.imageWithColor(UIColor.clearColor())
navigationBarAppearance.setBackgroundImage(clearImage, forBarMetrics: UIBarMetrics.Default)
navigationBarAppearance.shadowImage = clearImage
navigationBarAppearance.translucent = true
Same result, but the Inspector is a little different:
Apple Documents: https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UINavigationBar_Class/index.html#//apple_ref/occ/instp/UINavigationBar/shadowImage
The default value is nil, which corresponds to the default shadow
image. When non-nil, this property represents a custom shadow image to
show instead of the default. For a custom shadow image to be shown, a
custom background image must also be set with the
setBackgroundImage:forBarMetrics: method. If the default background
image is used, then the default shadow image will be used regardless
of the value of this property.
So basically you need to set background image before setting shadowImage to make it work.
Edit
Image generated from color (Swift) as background Navigation. Not sure if your blur function will still work when you change backgroundImage for Navigation bar. That would be a different problem.
class func imageWithColor(color: UIColor) -> UIImage {
let rect = CGRectMake(0, 0, 1, 1)
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0)
color.setFill()
UIRectFill(rect)
var image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
Have you tried navigationBar.shadowImage = [UIImage new]; ?

Resources