0

In my app have two UIViewControllers (first, where user choose character, and second, where characters info is shown). Trying do everything from code. So my code:

AppDelegate.swift:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    self.window = UIWindow(frame: UIScreen.main.bounds)
    guard let window = self.window else {
        return false
    }
    UIApplication.shared.statusBarStyle = .lightContent

    let viewController = ViewController()
    let navigationController = UINavigationController(rootViewController: viewController)

    window.rootViewController = navigationController
    window.makeKeyAndVisible()

    return true
}

ViewController: UIViewController

...
    func buttonPressed(sender: UIButton) {
        guard let typeName = sender.title(for: .normal), let unitType = UnitType(rawValue: typeName) else {
            return
        }
        let unitViewController = UnitViewController(unitType: unitType)
        let navigationController = UINavigationController(rootViewController: unitViewController)
        self.present(navigationController, animated: true, completion: nil)
    }

And UnitViewController: UIViewController (where chosen character's info is shown):

...
    fileprivate func setupNavigationBar() {
        let backButton = UIBarButtonItem(title: "<", style: .plain, target: self, action: #selector(backToViewController(sender:)))
        self.navigationItem.setLeftBarButton(backButton, animated: true)
        self.navigationItem.titleView?.backgroundColor = AppColor.Black.color
        self.navigationItem.titleView?.tintColor = .white
        self.navigationItem.title = "\(self.unitType.rawValue)"
    }

    func backToViewController(sender: AnyObject) {
        self.dismiss(animated: true) {
            let viewController = ViewController()
            let navigationController = UINavigationController(rootViewController: viewController)
            self.present(navigationController, animated: true, completion: nil)
        }
    }

So I have two screens:

enter image description here enter image description here

Well, and I have some questions: 1. Getting

2016-12-02 05:48:21.855 The API Awakens[16402:551729] Warning: Attempt to present on whose view is not in the window hierarchy! warning when press 'back' button in UnitViewController. What I am doing wrong?

  1. Gray background color and black color of NavigationBar. How change it to black and white?

  2. How get 'system' back button for UIBarButtonItem, not just .plain with title "<"? So my UnitViewController navigation bar should looks like this: enter image description here

Any help will be greatly appreciated!

UPDATE 1: 1. Warning gone, thanks to @dip 2. Made navigation bar dark, thanks to @aznelite89. But, there is my code in AppDelegate.swift:

UINavigationBar.appearance().barTintColor = AppColor.Black.color

AppColor.Black is exact same color I'm using for background in ViewController, but that how it looks now:

enter image description here

Looks like alpha of NavigationBar is not 1.0...

UPDATE 2: Different between color of NavigationBar and color I've used is 13 in RGB values, so I've hacked it setting color of NavigationBar with RGB value less by 13 than original color... It's ok now

1
  • 1
    It is not needed to change the root view controller every time. instead of this let unitViewController = UnitViewController(unitType: unitType) let navigationController = UINavigationController(rootViewController: unitViewController) self.present(navigationController, animated: true, completion: nil) you can do let unitViewController = UnitViewController(unitType: unitType) self.navigationController?.pushViewController(unitViewController, animated: true) Commented Dec 2, 2016 at 3:06

1 Answer 1

1
  1. Try this :-

    let viewController = ViewController()
    viewController.modalPresentationStyle = .overCurrentContext
    window?.rootViewController?.present(viewController, animated: true, completion: nil)
    
  2. Add this in app delegate.swift :-

    UINavigationBar.appearance().barTintColor = UIColor.black
    
  3. You may change these attributes on your storyboard attribute inspector

Sign up to request clarification or add additional context in comments.

5 Comments

2. UINavigationBar.appearance().barTintColor = UIColor.black works 3. Nope, I have get rid of Main.storyboard and changes in LaunchScreen.storyboard doesnt have any effect
And strange thing setting UINavigationBar.appearance().barTintColor to .black doesnt make NavigationBar black, it have black, but lighter color (different from main view)
launch screen.storyboard only affect the launch screen
@zzheads you may set the specify color with RGB value , i am using custom rgb method something like UIcolor.rgb(red:255 ,green:255 ,blue:255)
@zzheads in your ViewController 's viewDidLoad() , add this code : navigationController?.navigationBar.isTranslucent = false

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.