I want both menu in Navigation bar extension and hide the navigation bar except the extension when scrolling.
I was able to achieve menu with PageMenu "PageMenu" and hiding the navigation bar through AMScrollingNavbar
But the problem I am facing now is Nav bar hides but the views does not move up with nav bar
func loadControllers(){
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let topViewController = storyboard.instantiateViewControllerWithIdentifier("TopNewsController") as!
TopNewsController
topViewController.title = "TOP"
controllerArray.append(topViewController)
let briefViewController = storyboard.instantiateViewControllerWithIdentifier("BriefViewController") as!
BriefViewController
briefViewController.title = "BRIEFS"
controllerArray.append(briefViewController)
let videoViewController = storyboard.instantiateViewControllerWithIdentifier("VediosViewController") as!
VediosViewController
videoViewController.title = "VIDEOS"
controllerArray.append(videoViewController)
// Customize menu
parameters = [
.ScrollMenuBackgroundColor(UIColor.navigationBarColor()),
.ViewBackgroundColor(UIColor(red: 20.0/255.0, green: 20.0/255.0, blue: 20.0/255.0, alpha: 1.0)),
.SelectionIndicatorColor(UIColor.whiteColor()),
.BottomMenuHairlineColor(UIColor(red: 70.0/255.0, green: 70.0/255.0, blue: 80.0/255.0, alpha: 1.0)),
.MenuItemFont(UIFont(name: "HelveticaNeue-Bold", size: 14.0)!),
.MenuHeight(expressTribuneUtilities.convertIphone6ToIphone5(48)),
.MenuItemWidth(90.0),
.CenterMenuItems(true)
]
AppDelegate.getInstatnce().nav = self.navigationController as! ScrollingNavigationController
AppDelegate.getInstatnce().nav.scrollingNavbarDelegate = self
// Initialize scroll menu
pageMenu = CAPSPageMenu(viewControllers: controllerArray, frame: CGRectMake(0.0, 64.0, self.view.frame.width, self.view.frame.height), pageMenuOptions: parameters)
self.view.addSubview(pageMenu!.view)
}
Call the Scroll method from the class thats added in the menu
AppDelegate.getInstatnce().nav.followScrollView(tableMainView, delay: 0.5)
AppDelegate.getInstatnce().nav = self.navigationController as! ScrollingNavigationController
AppDelegate.getInstatnce().nav.scrollingNavbarDelegate = self
change both line to like this it wont work because you are setting delegate for other instance instead of your navigation .
let navController = AppDelegate.getInstatnce().window?.rootViewController as! ScrollingNavigationController
navController.scrollingNavbarDelegate = self
implement delgate in your controller and set origin of your view w.r.t your
scrollview.
Related
How can I set another ViewController for the searchResultsUpdater?
I tried to do it like this, but the results are not updating.
let searchNav = storyboard!.instantiateViewController(withIdentifier: "SearchControllerNav") as! UINavigationController
let vc = searchNav.topViewController as! PageMenuVC
let searchVC = storyboard!.instantiateViewController(withIdentifier: "SearchVC") as! SearchVC
self.searchController = UISearchController(searchResultsController: vc)
vc.searchController.searchResultsUpdater = searchVC
Here's my PageMenuVC:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
var controllerArray : [UIViewController] = []
let searchVC = storyboard?.instantiateViewController(withIdentifier: "SearchVC")
controllerArray.append(searchVC!)
let testVC = storyboard?.instantiateViewController(withIdentifier: "test")
controllerArray.append(testVC!)
// Customize menu (Optional)
let parameters: [CAPSPageMenuOption] = [
.scrollMenuBackgroundColor(UIColor(red: 30.0/255.0, green: 30.0/255.0, blue: 30.0/255.0, alpha: 1.0)),
.viewBackgroundColor(UIColor(red: 20.0/255.0, green: 20.0/255.0, blue: 20.0/255.0, alpha: 1.0)),
.selectionIndicatorColor(UIColor.orange),
.bottomMenuHairlineColor(UIColor(red: 70.0/255.0, green: 70.0/255.0, blue: 80.0/255.0, alpha: 1.0)),
.menuItemFont(UIFont(name: "HelveticaNeue", size: 13.0)!),
.menuHeight(40.0),
.menuItemWidth(90.0),
.centerMenuItems(true)
]
// Initialize scroll menu
pageMenu = CAPSPageMenu(viewControllers: controllerArray, frame: CGRect(x: 0.0, y: 0.0, width: self.view.frame.width, height: self.view.frame.height), pageMenuOptions: nil)
self.addChildViewController(pageMenu!)
self.view.addSubview(pageMenu!.view)
pageMenu!.didMove(toParentViewController: self)
}
In SearchVC I have this function:
//MARK: Filter
func filterContentForSearchText(searchText: String, scope: String = "All") {
filteredUsers = allUsers.filter { user in
return user.name.lowercased().contains(searchText.lowercased())
}
collectionView.reloadData()
}
//MARK: SearchResultDelegate
func updateSearchResults(for searchController: UISearchController) {
let searchBarText = searchController.searchBar.text!
filterContentForSearchText(searchText: searchBarText)
self.searchController = searchController
collectionView.reloadData()
}
If you need more info, let me know!
I think you need to keep the instantiated view controllers in a (private) property of the self view controller: If you set it as the the delegate to vc.searchController.searchResultsUpdater, this is only a weak reference, therefore if you do not store searchVC in self, it will be destroyed too soon (just at the end of the code block). The same holds for searchNav (but I think this will be presented, therefore I'll skip it below):
class MyVC : UIViewController {
var searchVC:UIViewController?
// ...
func XYZ() {
let searchNav = storyboard!.instantiateViewController(withIdentifier: "SearchControllerNav") as! UINavigationController
let vc = searchNav.topViewController as! PageMenuVC
// Store searchVC in property:
self.searchVC = storyboard!.instantiateViewController(withIdentifier: "SearchVC") as! SearchVC
self.searchController = UISearchController(searchResultsController: vc)
vc.searchController.searchResultsUpdater = searchVC
self.present(searchNav, animated:true, completion:nil)
}
}
I add NavigationBar programmatically and later added title to it, but it doesn't appear at all. What is the problem here?
let navigationBar = UINavigationBar(frame: CGRectMake(0, 0, self.view.frame.size.width, 55))
navigationBar.barTintColor = UIColor(red: 44/255, green: 54/255, blue: 63/255, alpha: 1)
navigationController?.navigationItem.title = "AAA"
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor()]
view.addSubview(navigationBar)
navigationBar appears, but title not. How to fix it?
I've tried this too:
navigationBar.topItem?.title = "BBB"
nothing again
Hope this will help you out.
// Create the navigation bar
let navigationBar = UINavigationBar(frame: CGRectMake(0, 0, self.view.frame.size.width, 64))
// Offset by 20 pixels vertically to take the status bar into account
navigationBar.backgroundColor = UIColor.whiteColor()
// Create a navigation item with a title
let navigationItem = UINavigationItem()
navigationItem.title = "Title"
// Assign the navigation item to the navigation bar
navigationBar.items = [navigationItem]
// Make the navigation bar a subview of the current view controller
self.view.addSubview(navigationBar)
This code is working for me.
UPDATE : Swift 4/Swift 5
// Create the navigation bar
let navigationBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 64))
// Offset by 20 pixels vertically to take the status bar into account
navigationBar.backgroundColor = UIColor.white
// Create a navigation item with a title
let navigationItem = UINavigationItem()
navigationItem.title = "Title"
// Assign the navigation item to the navigation bar
navigationBar.items = [navigationItem]
// Make the navigation bar a subview of the current view controller
self.view.addSubview(navigationBar)
Navigation bar title is set in the view controller being displayed. Normally this is done in view did load on the view controller:
override func viewDidLoad() {
super.viewDidLoad()
self.title = "AAA"
}
EDIT: After realising that OP is adding UINavigationBar on to UIViewController and not using standard UINavigationController:
let navigationBar = UINavigationBar(frame: CGRectMake(0, 0, self.view.frame.size.width, 55))
navigationBar.barTintColor = UIColor(red: 44/255, green: 54/255, blue: 63/255, alpha: 1)
let navigationItem = UINavigationItem.init(title: "AAA")
navigationBar.items = [navigationItem]
view.addSubview(navigationBar)
I am trying to fill the status bar background color to orange using the following
UINavigationBar.appearance().tintColor = UIColor.orangeColor()
UINavigationBar.appearance().barTintColor = UIColor.orangeColor()
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: true)
However, I get a white status bar that should be filled with orange instead from following this example: Customize navigation bar appearance with swift
I am setting this up in the AppDelegate.swift file under didFinishLaunchingWithOptions method to apply it to the entire app.
I have edited my info.plist to the following: View controller-based status bar appearance => NO
Does anyone know what I am doing wrong?
Edit: I'm not sure if it matters but the view is in a UITabBarController
Edit 2: This is happening in all the views actually, not just the UITabBarController.
Edit 3: Thanks #Utsav Parikh
I am adding a view now on top of the status bar and it for a brief moment while the app loads the status bar is orange but, once it finishes loading it gets pushed OFF the view and replaced with the generic white status bar.
Why would this be happening?
let view = UIView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.mainScreen().bounds.size.width, height: 20.0))
view.backgroundColor=UIColor.orangeColor()
self.window!.rootViewController!.view.addSubview(view)
Edit for Swift 3:
with UITabBarController
let view = UIView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.main.bounds.size.width, height: 20.0))
view.backgroundColor = .orange
self.view.addSubview(view)
Without embedded controllers
I realize some people come here not only for the status bar, but actually the navigation bar, so I learned a few tricks along the way to do it without any embedded controllers:
Add this method in your AppDelegate.swift and call it in the didFinishLaunchingWithOptions
func customizeAppearance() {
UINavigationBar.appearance().barTintColor = UIColor.black
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
UITabBar.appearance().barTintColor = UIColor.black
let tintColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0)
UITabBar.appearance().tintColor = tintColor
}
Edit for Swift 3:
With UITabBarController
let view = UIView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.main.bounds.size.width, height: 20.0))
view.backgroundColor = .orange
self.view.addSubview(view)
Without embedded controllers
I realize some people come here not only for the status bar, but actually the navigation bar, so I learned a few tricks along the way to do it without any embedded controllers:
Add this method in your AppDelegate.swift and call it in the didFinishLaunchingWithOptions
func customizeAppearance() {
UINavigationBar.appearance().barTintColor = UIColor.black
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
UITabBar.appearance().barTintColor = UIColor.black
let tintColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0)
UITabBar.appearance().tintColor = tintColor
}
Thanks to #Utsav I added the following subview to my UITabBarController and this seems to be working now:
let view = UIView(frame:
CGRect(x: 0.0, y: 0.0, width: UIScreen.mainScreen().bounds.size.width, height: 20.0)
)
view.backgroundColor = UIColor.orangeColor()
self.view.addSubview(view)
The UITabBarController doesn't seem to play well in AppDelegate. If anyone has a better way let me know but, as of now this is the solution I have come around to.
Add this code in didFinishLaunchingWithOptions in AppDelegate
let view = UIView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.mainScreen().bounds.size.width, height: 20.0))
view.backgroundColor=UIColor.orangeColor()
self.window.rootViewController.view.addSubview(view)
Hope it helps you....!!!
This is how I did it without adding a view in a VC with in a NavBarController
I wanted the color of the status bar to be the same as the VC view color so I just wrote:
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.grayColor()
self.navigationController?.navigationBar.clipsToBounds = true
}
Try it.
I think your last line is reverting your changes, try this:
override func viewWillAppear(animated: Bool) {
UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: true)
super.viewWillAppear(animated)
var nav = self.navigationController?.navigationBar
nav?.barStyle = UIBarStyle.Black
nav?.tintColor = UIColor.orangeColor()
nav?.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
}
After what u did in info.plist to the following: View controller-based status bar appearance => NO.
Add this code in AppDelegate.swift file under didFinishLaunchingWithOptions:
var navigationBarAppearace = UINavigationBar.appearance()
navigationBarAppearace.tintColor = uicolorFromHex(0xffffff)
navigationBarAppearace.barTintColor = uicolorFromHex(0x2E9AFE)
// change navigation item title color
navigationBarAppearace.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]
UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent
and u can select any hex code for ur choice of color..!! Enjoy..!!
Sorry, forgot to use hexcode you will be needing this also so add this code anywhere in your AppDelegate.swift:
func uicolorFromHex(rgbValue:UInt32)->UIColor {
let red = CGFloat((rgbValue & 0xFF0000) >> 16)/256.0
let green = CGFloat((rgbValue & 0xFF00) >> 8)/256.0
let blue = CGFloat(rgbValue & 0xFF)/256.0
return UIColor(red:red, green:green, blue:blue, alpha:1.0)
}
Simon's answer in swift 3
let view = UIView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.main.bounds.size.width, height: 20.0))
view.backgroundColor = .orange
self.view.addSubview(view)
There is one other way I know which uses private api. This has some benefits when orientation changes and keyboard is presented and view move up. I've used it and was lucky every time (app was released in the app store).
func setStatusBarBackgroundColor(color: UIColor) {
guard let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView else { return }
statusBar.backgroundColor = color
}
Swift 3:
In your AppDelegate.swift file paste the code bellow into your didFinishLaunchingWithOptions method:
let view = UIView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.main.bounds.size.width, height: 20.0))
view.backgroundColor = UIColor(red: 255/255, green: 130/255, blue: 0/255, alpha: 1.0) // Organge colour in RGB
self.window?.rootViewController?.view.addSubview(view)
This works fine for me!
There is a main difference in tintColor and changing the background color of UINavigationBar. The best way in my opinion is apply a background image, made by 1 pixel square image of just one color.
Like that:
let tabbarAndNavBarBkg = UIImage(named: "nav_tab")
UINavigationBar.appearance().setBackgroundImage(tabbarAndNavBarBkg, forBarMetrics: .Default)
Or you can create a category on UIColor to return a UIImage given a UIColor instance, in objC:
+ (UIImage *) imageWithColor:(UIColor*) color {
CGRect rect = CGRectMake(0, 0, 1, 1);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, color.CGColor);
CGContextFillRect(context, rect);
UIImage *colorImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return colorImage;
}
UINavigationBar.appereance() works for upcoming viewControllers, but not the currently displayed rootViewController. To achieve this I have added the following to my didFinishLaunchingWithOptions:
UINavigationBar.appearance().tintColor = myColor
let navigationController = UIApplication.sharedApplication().windows[0].rootViewController as! UINavigationController
navigationController.navigationBar.barTintColor = myColor
navigationController.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : myTextColor]
navigationController.navigationBar.translucent = false
navigationController.setNeedsStatusBarAppearanceUpdate()
I have a MainView Controller and a second SettingsView Controller.
In SettingsView controller, I let user select a background color, save it and move back to MainView Controller.
I use Navigation controller segue to move from Main to Settings and use dismissViewControllerAnimated to move back to Main.
My problem is when I set background for main view, it doesn't show up.
But if I close and restart app then it comes up correctly.
Is it because Main view is already open and not refreshed back?
Here is the code:
Settings:
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject("theme", forKey: "BackgroundColor")
Main:
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.clearColor()
var backgroundLayer = Util.GetTheme()
backgroundLayer.frame = view.frame
view.layer.insertSublayer(backgroundLayer, atIndex: 0)
}
As per your requirement, you need to move the code for changing color to viewWillAppear method. Since viewDidLoad will be called only once at the time when it get loaded to memory. viewWillAppear will be called each time it will come to foreground or visible to user.
But where you are saving selected color and applying the saved color?
Try This code :
class Colors {
let colorTop = UIColor(red: 192.0/255.0, green: 38.0/255.0, blue: 42.0/255.0, alpha: 1.0).CGColor
let colorBottom = UIColor(red: 35.0/255.0, green: 2.0/255.0, blue: 2.0/255.0, alpha: 1.0).CGColor
let gl: CAGradientLayer
init() {
gl = CAGradientLayer()
gl.colors = [ colorTop, colorBottom]
gl.locations = [ 0.0, 1.0]
}
}
I wan to stick my navigation bar on top for every pages that i have in app.
I have my Code below
navigationController?.hidesBarsOnSwipe = true
navigationController?.hidesBarsOnTap = true
navigationController?.navigationBar.barStyle = UIBarStyle.BlackTranslucent
navigationController?.navigationBar.opaque = true
navigationController?.navigationBar.translucent=true
navigationController?.navigationBar.alpha = 0.4
navigationController?.navigationBar.backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.4)
navigationController?.navigationBar.translucent = true
navigationController?.navigationBar.tintColor = UIColor.whiteColor()
self.navigationController?.setNavigationBarHidden(false, animated: false)
What can i do to just stick navigation bar?
Thanks,
Dhaval.
Ok, here some guide, assume that you're a do-everything-by-code guy.
In app delegate:
let tempVC = UIViewController()
tempVC.backgroundColor = UIColor.redColor()
let navVC = UINavigationController(rootViewController:tempVC)
window?.rootViewController = navVC
it should work.
Then you do your navigationBar customization code in here or in your viewcontroller.
Issue resolved
Code below
navigationController?.navigationBar.tintColor = UIColor.whiteColor()
navigationController?.navigationBar.barStyle = UIBarStyle.BlackTranslucent
navigationController?. navigationBar. hidden=false