Custom Tab Bar Background Image is not showing correctly - ios

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

Related

How to resize UINavigationBarAppearance background image

I am trying to add a background image to my nav bar using the UINavigationBarAppearance. However, the imageView size is bigger than the navigation bar content view.. How do I fix this?
Result: Notice the imageview overlaps with the safearea
Heiarchy debugger
Code:
private func setupNavBar() {
navigationItem.largeTitleDisplayMode = .never
guard let navigationController = navigationController else { return }
let appearance = navigationController.navigationBar.standardAppearance.copy()
appearance.configureWithTransparentBackground()
appearance.backgroundImage = UIImage.checkmark
appearance.backgroundImageContentMode = .scaleAspectFit
appearance.backgroundColor = .red
navigationController.navigationBar.standardAppearance = appearance
}
Found an alternative. Instead of using the appearance background image, I set the titleView on the navigation item.
let imageView = UIImageView(image: UIImage.checkmark)
imageView.contentMode = .scaleAspectFit
navigationItem.titleView = imageView

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

Remove border between View and Search Bar

So in Xcode I'm trying to create a seamless search bar. So I'm trying to replicate something like this
Note how the status bar is the same color as the search bar. Now here's the result to my approach.
What I did was add a View to cover up the default status bar with the blue background. Then I added a search bar and changed it's background to blue. For some reason I end up getting a black border between the two, which ruins the "seamless" design. Any ideas on how I can remove the black border in Swift?
For iOS 7+:
searchBar.backgroundImage = UIImage()
Otherwise this will work on all iOS versions:
searchBar.layer.borderWidth = 1
searchBar.layer.borderColor = thatBlueColor.CGColor
Swift 4
searchBar.barTintColor = UIColor.white
searchBar.setBackgroundImage(UIImage.init(), for: UIBarPosition.any, barMetrics: UIBarMetrics.default)
Sample image
Upate Sample code for navigation bar and search bar background color:
Navigation bar color
self.navigationController?.navigationBar.barTintColor = .blue
Search bar color
searchBarProperty.backgroundColor = self.navigationController?.navigationBar.barTintColor
Note : Navigation bar and search bar color must be same.
Sample image with blue navigation bar and blue search bar
In Xcode 8.3 and Swift 3
Create an outlet from your search bar to your ViewController (I called mine searchBarOutlet for this example).
Below viewDidLoad insert the following.
self.searchBarOutlet.backgroundImage = UIImage()
You should have the following:
override func viewDidLoad() {
super.viewDidLoad()
self.searchBarOutlet.backgroundImage = UIImage()
When you run your app the lines will be gone (they will still be visible on storyboard).
In my case, beyond the edge of search bar needed to take the edge off also the navigation bar.
C# code:
NavigationController.NavigationBar.ShadowImage = new UIImage();
NavigationController.NavigationBar.SetBackgroundImage (new UIImage (), UIBarMetrics.Default);
Swift code:
self.navigationController.navigationBar.shadowImage = UIImage()
self.navigationController.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)
The best solution to remove top and bottom default borders is:
To set a new empty searchBar background layout in viewDidLoad for example:
searchBar.backgroundImage = UIImage()
I found these answers to be more complicated than they needed to be. You can just modify the constraint that is binding the searchBar view and the other view to -1pts so that it overlaps exactly by the height of the searchBar's margin.
I encountered the same situation when I set the statusBar and searchBar translucent.
In this situation, I couldn't resolve with the answers written here however I could solve by the following approach.
put UIVisualEffectView on self.view (view of your VC)
make custom class of searchBar, which background is transparent
(also let statusBar transparent)
(swift4 code)
class TransparentSearchBar: UISearchBar {
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
makeTransparentBackground()
}
private func makeTransparentBackground() {
for view in self.subviews {
view.backgroundColor = UIColor.clear
for subview in view.subviews {
if let imageview = subview as? UIImageView {
imageview.image = nil
}
}
}
}
}
somewhere in viewDidLoad (statusBar)
let statusWindow = UIApplication.shared.value(forKey:"statusBarWindow") as! UIView
let statusBar = statusWindow.subviews[0] as UIView
statusBar.backgroundColor = UIColor.clear
in Xcode 13
select the search bar and change the search Style to Minimal

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