UIPageViewController indicators don't change color - ios

I've read many other threads here about this problem, it appears that the best solution now is to set it as follows:
let pageControl = UIPageControl.init(frame: CGRectMake(0, UIScreen.mainScreen().bounds.size.height*14/15, UIScreen.mainScreen().bounds.size.width, UIScreen.mainScreen().bounds.size.height))
pageControl.backgroundColor = UIColor(red: 0, green: 147/255, blue: 229/255, alpha: 1)
pageControl.pageIndicatorTintColor = UIColor.lightGrayColor()
pageControl.currentPageIndicatorTintColor = UIColor.blackColor()
view.addSubview(pageControl)
But for some reason, this does not work for me. Page control background changes its color, but the indicators remain white.
My entire viewDidLoad() method
override func viewDidLoad() {
super.viewDidLoad()
self.pageViewController = self.storyboard!.instantiateViewControllerWithIdentifier("OnboardingPageViewController") as! UIPageViewController
self.pageViewController.dataSource = self
let startingViewController:OnboardingPageContentViewController = self.viewControllerAtIndex(0)!
let viewControllers:Array<OnboardingPageContentViewController> = [startingViewController]
self.pageViewController.setViewControllers(viewControllers, direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil)
let pageControl = UIPageControl.init(frame: CGRectMake(0, UIScreen.mainScreen().bounds.size.height*14/15, UIScreen.mainScreen().bounds.size.width, UIScreen.mainScreen().bounds.size.height))
pageControl.backgroundColor = UIColor(red: 0, green: 147/255, blue: 229/255, alpha: 1)
pageControl.pageIndicatorTintColor = UIColor.lightGrayColor()
pageControl.currentPageIndicatorTintColor = UIColor.blackColor()
view.addSubview(pageControl)
self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)
self.addChildViewController(pageViewController)
self.view.addSubview(pageViewController.view)
self.pageViewController.didMoveToParentViewController(self)
}
Thanks in advance

it appears that the best solution now is to set it as follows:
No, it isn't. You are adding a new UIPageControl, but the UIPageViewController already has a UIPageControl. What you want is to set the attributes of that UIPageControl.
The way to do that is to use the appearance proxy. Example:
let proxy = UIPageControl.appearance()
proxy.pageIndicatorTintColor = UIColor.redColor().colorWithAlphaComponent(0.6)
proxy.currentPageIndicatorTintColor = UIColor.redColor()
proxy.backgroundColor = UIColor.yellowColor()

Related

UITextView with border and floating placeholder using MDCMultilineTextField

Im trying to achieve as in these images below
On User edit
here is my code
#IBOutlet weak var commentTxtField: MDCMultilineTextField! // Connected to storyboard
commentTxtField.textView?.delegate = self
commentTxtField.textView?.frame = CGRect(x: (commentTxtField.textView?.frame.origin.x)!, y: (commentTxtField.textView?.frame.origin.y)!, width: (commentTxtField.textView?.frame.width)!, height: CGFloat(GenUtils.shared.getHeightForPercent(percent: 11.99)))
commentTxtField.expandsOnOverflow = false
commentTextFieldController = MDCTextInputControllerOutlinedTextArea(textInput: commentTxtField)
commentTextFieldController?.placeholderText = "Comment"
commentTextFieldController?.isFloatingEnabled = true
commentTextFieldController!.characterCountMax = UInt(maxCharactersCount)
commentTextFieldController?.characterCountViewMode = UITextField.ViewMode.never
commentTextFieldController?.activeColor = UIColor.white.withAlphaComponent(0.6)
commentTextFieldController?.normalColor = UIColor.white.withAlphaComponent(0.2)
// emailTextFieldController?.borderFillColor = UIColor.white
commentTextFieldController?.floatingPlaceholderActiveColor = UIColor(red: 249/255, green: 249/255, blue: 249/255, alpha: 0.54)
commentTextFieldController?.inlinePlaceholderColor = UIColor.white
commentTextFieldController?.floatingPlaceholderNormalColor = UIColor(red: 249/255, green: 249/255, blue: 249/255, alpha: 0.54)
commentTxtField.textColor = UIColor.white
commentTextFieldController?.inlinePlaceholderFont = UIFont(name:"Avenir-Medium",size:16)
tried to set the textviewframe, but not reflecting on screen. And also not able to get floating placeholder on border line align. What am i missing?
use **MDCTextField** as below:
class SomeVC: UIViewController, UITextFieldDelegate {
var textFieldControllerFloating = MDCTextInputControllerUnderline()
override func viewDidLoad() {
let textFieldFloating = MDCTextField()
self.view.addSubview(textFieldFloating)
//place textfield where you want
textFieldFloating.placeholder = "Some cool animating placeholder"
textFieldFloating.delegate = self
// This will animate the textfield's place holder
textFieldControllerFloating = MDCTextInputControllerUnderline(textInput: textFieldFloating)
}
}
also if you want an outlined textfield you to use "Outlined" text field
https://material.io/develop/ios/components/textfields/

Bring navbar in front of the UISearchController, Swift

I used a navBar which is created programmatically with an UISearchController.
When I start editing the UISearchBar:
- the navBar stays behind the dim view
- the tableView hides half of the navbar
- when the tabeView appeared, the cancel buttons are not selectable.
My UISearchController :
let locationSearchTable = storyboard!.instantiateViewControllerWithIdentifier("SearchTableViewController") as! SearchTableViewController
resultSearchController = UISearchController(searchResultsController: locationSearchTable)
resultSearchController.searchResultsUpdater = locationSearchTable
resultSearchController.view.backgroundColor=UIColor.clearColor()
locationSearchTable.delegate = self
let searchBar = self.resultSearchController.searchBar
searchBar.sizeToFit()
searchBar.placeholder = "Search"
searchBar.searchBarStyle = .Minimal
searchBar.barStyle = .Default
searchBar.translucent = true
searchBar.barTintColor = UIColor.whiteColor()
searchBar.setImage(UIImage(named:"search"), forSearchBarIcon: .Search, state : .Normal)
searchBar.delegate=self
resultSearchController.hidesNavigationBarDuringPresentation = false
resultSearchController.dimsBackgroundDuringPresentation = true
definesPresentationContext = true
My navBar :
let newNavBar : UINavigationBar = UINavigationBar.init(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, 64.0))
func styleNavBar (){
UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.Default
self.navigationController?.setNavigationBarHidden(true, animated: false)
let newItem : UINavigationItem = UINavigationItem.init()
newItem.titleView = self.resultSearchController.searchBar
newNavBar.barTintColor = UIColor(red: 243/255, green: 242/255, blue: 238/255, alpha: 1.0)
newNavBar.translucent = false
if let font = UIFont(name: "Avenir-Black", size: 16.0) {
let navBarAttributesDictionary : [String : AnyObject]? = [
NSForegroundColorAttributeName: UIColor(red: 74/255, green: 74/255, blue: 74/255, alpha: 0.51),
NSFontAttributeName: font
]
newNavBar.titleTextAttributes = navBarAttributesDictionary
}
newNavBar.setItems([newItem], animated: false)
self.view.addSubview(newNavBar)
self.view.bringSubviewToFront(newNavBar)
}
How can I fix it, please?
Thanks!
Try using this :
self.navigationController?.navigationBar.layer.zPosition = 1
you can use stackview for it . select search and tableview and click on stackview and constraint stackview from top/left/right/bottom

UISearchController with Google Maps using Swift

I'm trying to create a screen where there is going to be a Google map covering the whole screen and a button on the top showing the selected address (where the marker is pointing).
And I want when the user clicks on that button to be presented with a UISearchBar where he can search for locations using the Google Places API.
I have been following the following tutorial:
https://www.youtube.com/watch?v=gRQUoHleCGM
Unfortunately, in my case the search bar is not appearing (although the background is dimmed when I press the button).
Here are some screenshots from before I press the button and after.
And this is the code of my view controller.
class ChooseLocationViewController: UIViewController, GMSMapViewDelegate, UISearchBarDelegate, UISearchControllerDelegate {
let geocoder = GMSGeocoder()
var mapView: GMSMapView = GMSMapView.mapWithFrame(CGRectZero, camera:GMSCameraPosition.cameraWithLatitude(51.515339, longitude: -0.141838, zoom: 16))
var addressButton: UIButton = UIButton()
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
let marker = UIImageView(frame: CGRect(x: 20, y: 20, width: 50, height: 50))
marker.image = UIImage(named: "default-marker")
marker.center.x = self.view.center.x
marker.center.y = self.view.center.y - (self.navigationController?.navigationBar.frame.size.height)!
addressButton = UIButton(frame: CGRect(x: 20, y: 20, width: self.view.frame.size.width - 25, height: 47))
addressButton.center.x = self.view.center.x
addressButton.backgroundColor = UIColor(red: 248/255, green: 248/255, blue: 248/255, alpha: 1.0)
addressButton.layer.borderColor = UIColor(red: 178/255, green: 178/255, blue: 178/255, alpha: 1.0).CGColor
addressButton.layer.borderWidth = 1.0
addressButton.layer.cornerRadius = 5
addressButton.setTitleColor(UIColor(red: 68/255, green: 68/255, blue: 68/255, alpha: 1.0), forState: .Normal)
addressButton.titleLabel?.font = UIFont.systemFontOfSize(13)
addressButton.addTarget(self, action: "showLocationSearch", forControlEvents: .TouchDown)
self.view = mapView
self.view.addSubview(marker)
self.view.addSubview(addressButton)
}
func showLocationSearch() {
let searchController = UISearchController(searchResultsController: nil)
searchController.delegate = self
searchController.searchBar.delegate = self
self.presentViewController(searchController, animated: true, completion: nil)
}
func willPresentSearchController(searchController: UISearchController) {
self.navigationController?.extendedLayoutIncludesOpaqueBars = true
}
func willDismissSearchController(searchController: UISearchController) {
self.navigationController?.extendedLayoutIncludesOpaqueBars = false
}
func mapView(mapView: GMSMapView, idleAtCameraPosition position: GMSCameraPosition) {
let coordinate = position.target
geocoder.reverseGeocodeCoordinate(coordinate) { response, error in
if let address = response?.firstResult() {
if let lines = address.lines {
if (lines.count > 0) {
self.addressButton.setTitle(lines[0], forState: .Normal)
}
}
}
}
}
}
Also, the reason I'm changing the extendedLayoutIncludesOpaqueBars is because I noticed that in other views where I have a search bar, this is necessary for it to be presented properly. In this case, I get exactly the same behavior (search bar is not showing) whether I do that or not.
What am I doing wrong? Did anyone have a similar issue?

iOS Custom Status Bar Background Color not displaying

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()

Swift : Navigation bar should be stick on to the TOP

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

Resources