UItableviewcell is visible back to navigation bar in ios - ios

I am using the below code to set the gradient colour to navigationBar. But after applying the gradient colour my table view cell scrolls back to the navigationBar and content of the top cell is bit visible back to the navigationBar.
let gradient: CAGradientLayer = CAGradientLayer()
let navigationBar = self.navigationController?.navigationBar.bounds
let statusBar = UIApplication.sharedApplication().statusBarFrame
let gradientBounds = CGRectMake(0.0, 0.0, (navigationBar?.size.width)!+statusBar.size.width, (navigationBar?.size.height)!+statusBar.size.height)
gradient.frame = (gradientBounds)
gradient.colors = [UIColor.redColor().CGColor, UIColor.blackColor().CGColor]
gradient.locations = [0.0, 1.0]
UIGraphicsBeginImageContext(gradient.bounds.size);
gradient.renderInContext(UIGraphicsGetCurrentContext()!)
let backImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
self.navigationController?.navigationBar.barStyle = UIBarStyle.Default
self.navigationController?.navigationBar.setBackgroundImage(backImage, forBarMetrics: UIBarMetrics.Default)

Navigation bars are translucent by default and even if they were not - that'd still be the first place to verify. Setting a solid background image doesn't make it opaque, in addition to that you'll need to set the navigation bar translucent property through code or at the Interface Builder.

Related

Set background in UIViewController inside navigation controller

I want to set my background color fullscreen (includes navigation bar and status bar also) with gradient color. Right now, I made this by creating gradient
func setDefaultColorBackground() {
let colorTop = UIColor(hexString: "804b8a").cgColor
let colorBottom = UIColor(hexString: "42074b").cgColor
let gradientLayer = CAGradientLayer()
gradientLayer.colors = [ colorTop, colorBottom]
gradientLayer.locations = [ 0.0, 1.0]
gradientLayer.frame = self.view.bounds
self.view.layer.insertSublayer(gradientLayer, at: 0)
}
So I added layer in my view in ViewController. It works but it does't covered up the status bar and navigation bar. See this image:
I think the background only fill the view under navigation bar. Anyway, I am using .xib and load it up manually in my view controller class.
Is there any ideas how to do it? Thank you so much!
I guess to change the color of the navigation bar you have to write the code for it also something like this :
UINavigationBar.appearance().barTintColor = UIColor.redColor()
or whatever color you want to provide :
UINavigationBar.appearance().barTintColor = UIColor(red: 0, green: 0/255, blue: 205/255, alpha: 1)
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]
I hope it was helpful to you. Happy Coding :)

How to set background of a view (UITableview) with dynamic cells?

I am trying to set a background Colour to the view occupied by the UITableViewCell. I have dragged an outlet and have a gradient layer which i return to it
extension CAGradientLayer{
func viewGradient(topColour:UIColor,bottomColour : UIColor) -> CAGradientLayer{
let gradientColor: [CGColor] = [topColour.cgColor, bottomColour.cgColor]
let gradientLocations: [Float] = [0.0/1.0]
let gradientLayer: CAGradientLayer = CAGradientLayer()
gradientLayer.colors = gradientColor
gradientLayer.locations = gradientLocations as [NSNumber]?
return gradientLayer
}
}
And i access this method in my viewDidLoad method of a view controller
let bottomColour = UIColor(red: (0.018), green: (0.38), blue: (0.64), alpha: 1.0)
let topColour = UIColor(red: (0.13), green: (0.63), blue: (0.67), alpha: 0.7)
let background = CAGradientLayer().viewGradient(topColour:topColour,bottomColour : bottomColour)
background.frame = self.view.bounds
self.view.layer.insertSublayer(background, at: 0)
myMenu.layer.insertSublayer(background, at: 0)
I have dynamic cells in my table view and as the number of cells increases the view slides down and i Have to scroll to see my cells.I have white spaces. Is there any way i can set the entire scrolling region to have my background gradient colour or am i missing something ?
Here is the screenshot of the images showing my exact problem
As commented above, UITableView has a backgroundView property.
In your viewDidLoad method, keep your background constant declared as is, then below add:
self.tableView.backgroundView = background
Hope that helps.

How to make a Navigation Bar and Status Bar blurred (UIBlurEffect)? iOS, Swift 3

How to make the Navigation Bar and Status Bar blurred (UIBlurEffect)? When I'm by clicking on the image to scroll down (Scroll View) to other pictures, this picture (in this case with a white machine) is simply lost under the Navigation Bar, and it is necessary that this figure would be visible under the Navigation Bar with effect UIBlurEffect.
NO UIBlurEffect
I have tried so, but did not work:
func addBlurEffect() {
// Add blur view
let bounds = self.navigationController?.navigationBar.bounds as CGRect!
let visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .light))
visualEffectView.frame = bounds!
visualEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.navigationController?.navigationBar.addSubview(visualEffectView)
self.navigationController?.navigationBar.sendSubview(toBack: visualEffectView)
visualEffectView.isUserInteractionEnabled = false }
Firstly, when scrolling the picture just disappears under the Navigation Bar.
Second, Status Bar remains gray.
Bad UIBlurEffect for NavigationBar, and NO UIBlurEffect for StatusBar
In order to Status Bar not remain gray, I tried to do it, but to no avail =(
bounds.offsetBy(dx: 0.0, dy: -20.0)
bounds.size.height = bounds.height + 20.0
Also in AppDelegate (he application written in Objective-C) in didFinishLaunchingWithOptions, I tried to add it all remains the same without any changes:
[[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc]init] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setShadowImage:[[UIImage alloc]init]];
[[UINavigationBar appearance] setBackgroundColor:[UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.0f]];
[[UINavigationBar appearance] setTranslucent:YES];
Please help solve the problem, I mess around with it for 3 days.
Sorry for my bad English.
Setting the background image to the navigation bar does the trick. In swift 3 the below code works for me.
let visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .light))
visualEffectView.frame = (self.navigationController?.navigationBar.bounds.insetBy(dx: 0, dy: -10).offsetBy(dx: 0, dy: -10))!
self.navigationController?.navigationBar.isTranslucent = true
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.addSubview(visualEffectView)
output:
Going off #Vignesh answer it's a bad idea to hard code -10. For example this won't be sized correctly for iphone X.
// Find size for blur effect.
let statusBarHeight = UIApplication.shared.statusBarFrame.size.height
let bounds = self.navigationController?.navigationBar.bounds.insetBy(dx: 0, dy: -(statusBarHeight)).offsetBy(dx: 0, dy: -(statusBarHeight))
// Create blur effect.
let visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .light))
visualEffectView.frame = bounds
// Set navigation bar up.
self.navigationController?.navigationBar.isTranslucent = true
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.addSubview(visualEffectView)
self.navigationController?.navigationBar.sendSubview(toBack: visualEffectView)
I would also recommend creating a subclass of UINavigationController as it's good practice. Something like this:
final class func MyCustomNavigation: UINavigationController {
override func viewDidLoad() {
// Find size for blur effect.
let statusBarHeight = UIApplication.shared.statusBarFrame.size.height
let bounds = navigationBar.bounds.insetBy(dx: 0, dy: -(statusBarHeight)).offsetBy(dx: 0, dy: -(statusBarHeight))
// Create blur effect.
let visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .light))
visualEffectView.frame = bounds
// Set navigation bar up.
navigationBar.isTranslucent = true
navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationBar.addSubview(visualEffectView)
navigationBar.sendSubview(toBack: visualEffectView)
}
}
Swift 5, iOS 13 +
You can use UINavigationBarAppearance() in AppDelegate file. And don't forget set appearance for not scrolling mode. For me it works perfect with navigationBar.prefersLargeTitles.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let appearance = UINavigationBarAppearance()
appearance.configureWithTransparentBackground()
appearance.backgroundColor = UIColor.clear
appearance.backgroundEffect = UIBlurEffect(style: .light) // or dark
let scrollingAppearance = UINavigationBarAppearance()
scrollingAppearance.configureWithTransparentBackground()
scrollingAppearance.backgroundColor = .white // your view (superview) color
UINavigationBar.appearance().standardAppearance = appearance
UINavigationBar.appearance().scrollEdgeAppearance = scrollingAppearance
UINavigationBar.appearance().compactAppearance = scrollingAppearance
return true
}
Also you can check this link with test project https://github.com/leningradspb/BlurNavigationBar
Swift 3
Thanks to #Vignesh 's answer and #Romain Caron 's comment, I combined your code and the following code works for me in Swift 3
let visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .light))
visualEffectView.frame = (self.navigationController?.navigationBar.bounds.insetBy(dx: 0, dy: -10).offsetBy(dx: 0, dy: -10))!
self.navigationController?.navigationBar.isTranslucent = true
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.addSubview(visualEffectView)
self.navigationController?.navigationBar.sendSubview(toBack: visualEffectView)
Here is my Objective-C code for this same thing, and it includes support for the iPhone X's extra-large navigation bar.
UIVisualEffectView *fxView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
[fxView setFrame:CGRectOffset(CGRectInset(self.navigationController.navigationBar.bounds, 0, -12), 0, -60)];
[self.navigationController.navigationBar setTranslucent:YES];
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar insertSubview:fxView atIndex:1];

Blur effect on navigation bar while using a tableviewcontroller

Real time blur effect for Navigation Bar
Tried the solution mentioned in the above post,
AppDelegate.swift
// Sets background to a blank/empty image
UINavigationBar.appearance().setBackgroundImage(UIImage(), forBarMetrics: .Default)
// Sets shadow (line below the bar) to a blank image
UINavigationBar.appearance().shadowImage = UIImage()
// Sets the translucent background color
UINavigationBar.appearance().backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0)
// Set translucent. (Default value is already true, so this can be removed if desired.)
UINavigationBar.appearance().translucent = true
RootVC.swift
func addBlurEffect() {
// Add blur view
let bounds = self.navigationController?.navigationBar.bounds as CGRect!
let visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .Light))
visualEffectView.frame = bounds
visualEffectView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
self.navigationController?.navigationBar.addSubview(visualEffectView)
self.navigationController?.navigationBar.sendSubviewToBack(visualEffectView)
// Here you can add visual effects to any UIView control.
// Replace custom view with navigation bar in above code to add effects to custom view.
}
In viewDidLoad used self.addBlurEffect.
Problem, is status bar is still not blurred, and the blur effect is restricted only to RootVC.swift.
How can I extend it to all sub VCs?
Give this a shot:
bounds.offsetInPlace(dx: 0.0, dy: -20.0)
bounds.size.height = bounds.height + 20.0
Found it here

remove black border from UISearchController search bar swift

I have a UISearchController in my tableView. Also note that there is a navigation item in the top.
My issue is that i have a black border in the top and bottom when I load the page, however, it is not present when I click the search bar.
Search bar on page load (with black border):
After clicking on search bar (no black border):
Here is the code related:
let searchController = UISearchController(searchResultsController: nil)
in viewDidLoad:
searchController.searchBar.barTintColor = UIColor.redColor()
searchController.searchBar.tintColor = UIColor.whiteColor()
I followed several similar questions and made the following changes after the above lines in viewDidLoad():
1) searchController.searchBar.backgroundImage = UIImage()
2) searchController.searchBar.searchBarStyle = UISearchBarStyle.Minimal
3) searchController.searchBar.layer.borderColor = UIColor.clearColor().CGColor
4)
searchBar.layer.borderWidth = 1
searchBar.layer.borderColor = UIColor.whiteColor().CGColor
None worked. Is this an issue with the order I am using the code, or how would I get rid of these lines?
Fixed this after a lot of search. Here's how i did it. In viewDidLoad, added the following lines:
self.searchController.searchBar.translucent = false
self.searchController.searchBar.backgroundImage = UIImage()
self.searchController.searchBar.barTintColor = UIColor.redColor()
self.searchController.searchBar.tintColor = UIColor.whiteColor()
After that in app.delegate file in didFinishLaunchingWithOptions added the following code:
let backgroundColor = UIColor.redColor()
let foregroundColor = UIColor.whiteColor()
UIApplication.sharedApplication().statusBarStyle = .LightContent
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().setBackgroundImage(backgroundColor.toImage(), forBarMetrics: UIBarMetrics.Default)
UINavigationBar.appearance().tintColor = foregroundColor
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: foregroundColor]
Also need this extension on the app.delegate file (place outside of the class)
extension UIColor{
func toImage() -> UIImage {
let rect = CGRectMake(0, 0, 1, 1)
UIGraphicsBeginImageContextWithOptions(rect.size, true, 0)
self.setFill()
UIRectFill(rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
(Thanks to Noel for the extension taken from this answer)
And finally, the desired result:
I think it is the navigation bar you need to remove the border for.
How to hide iOS7 UINavigationBar 1px bottom line
For iOS 13 / Swift 5; the following line makes the trick:
UISearchBar.setBackgroundImage(UIImage(), for: .any, barMetrics: .default)
Cheers!

Resources