It is working for navigationBar:
var colour = UIColor.red
self.navigationController?.navigationBar.tintColor = colour
But do not work for toolbar:
self.navigationController?.toolbar.tintColor = colour
I searched the internet and stack overflow. No answer is workable for me.
Some people said:
self.toolbar.barTintColor = UIColor.redColor()
It is also not working for me. (value of type 'thisView' has no member 'toolbar')
I want to edit the toolbar color in coding. No change in the storyboard setting. Thanks.
EDIT:
I am working on adding a toolbar under the webview. Like go back, stop, reload.
//
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var webView: UIWebView!
var colour = UIColor.red
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.tintColor = colour
// problem in here ..........................
self.navigationController?.toolbar.tintColor = UIColor.black
let URL = NSURL(string: "https://www.apple.com")
webView.loadRequest(NSURLRequest(url: URL! as URL) as URLRequest)
}
#IBAction func backButton(_ sender: AnyObject) {
webView.goBack()
}
#IBAction func nextButton(_ sender: AnyObject) {
webView.goForward()
}
#IBAction func refreshButton(_ sender: AnyObject) {
webView.reload()
}
#IBAction func stopbutton(_ sender: AnyObject) {
webView.stopLoading()
}
}
If you've just got a toolbar that is on a ViewController in Your Storyboard all you need to do is add an IBOutlet to your View Controller and connect the toolbar in the storyboard to that outlet. This code goes in your ViewController
#IBOutlet var toolbar: UIToolbar?
Then, in the storyboard, hold the control button and click drag from View Controller (in the left sidebar) to your toolbar. This will create a connection between the toolbar in the storyboard to the toolbar var in your code. After that connection is made all you need to do is set the barTintColor on that toolbar variable like so:
self.toolbar.barTintColor = UIColor.blue
I wrote this function a few days ago to kinda workaround the fact that you can't change the color.
func setStatusBarColor(){
let view = UIView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.main.bounds.size.width, height: 20.0))
view.backgroundColor = UIColor(red: 81/255, green: 184/255, blue: 222/255, alpha: 1)
self.view.addSubview(view)
}
Also make sure to set the statusbar to .lightContent
UIApplication.sharedApplication().statusBarStyle = .LightContent
Related
I have created one popView with textfield and button in ViewController. if i click button then popView is appearing, and i am able to enter text in textfield and submit is working, and if i tap anywhere in view also i am able to remove popView, but here i want if i tap on anywhere in popView i don't want to dismiss popView, Please help me in the code.
here is my code:
import UIKit
class PopUPViewController: UIViewController {
#IBOutlet weak var popView: UIView!
#IBOutlet weak var inputField: UITextField!
#IBOutlet weak var textLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
popView.isHidden = true
// Do any additional setup after loading the view.
}
#IBAction func butnAct(_ sender: Any) {
view?.backgroundColor = UIColor(white: 1, alpha: 0.9)
popView.isHidden = false
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(PopUPViewController.dismissView))
view.addGestureRecognizer(tap)
}
#objc func dismissView() {
self.popView.isHidden = true
view?.backgroundColor = .white
}
#IBAction func sendButton(_ sender: Any) {
self.textLabel.text = inputField.text
}
}
In my code if i tap anywhere in the view popView is removing even if i tap on popView also its removing, i don't need that, if i tap on popView then popView need not to be remove.
Please help me in the code
You can override the touchesBegan method which is triggered when a new touch is detected in a view or window. By using this method you can check a specific view is touched or not.
Try like this
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
if touch?.view != self.popView {
dismissView()
}
}
func dismissView() {
self.popView.isHidden = true
view?.backgroundColor = .white
}
It's not the way I would have architected this, but to get around the problem you face you need to adapt your dismissView method so that it only dismisses the view if the tap is outside the popView.
To do this modify your selector to include the sender (the UITapGestureRecogniser )as a parameter:
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(PopUPViewController.dismissView(_:)))
and then in your function accept that parameter and test whether the tap is inside your view, and if so don't dismiss the view:
#objc func dismissView(_ sender: UITapGestureRegognizer) {
let tapPoint = sender.location(in: self.popView)
if self.popView.point(inside: tapPoint, with: nil)) == false {
self.popView.isHidden = true
view?.backgroundColor = .white
}
}
Your Popup view is inside the parent view of viewcontroller that's why on tap of popview also your popview is getting hidden.
So to avoid just add a view in background and name it bgView or anything what you want and replace it with view. And it will work fine .
Code:
#IBOutlet weak var bgView: UIView!//Add this new outlet
#IBOutlet weak var popView: UIView!
#IBOutlet weak var inputField: UITextField!
#IBOutlet weak var textLabel: UILabel!
#IBOutlet weak var submitButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
popView.isHidden = true
}
#IBAction func butnAct(_ sender: Any) {
bgView.backgroundColor = UIColor(white: 1, alpha: 0.9)//change view to bgView[![enter image description here][1]][1]
popView.isHidden = false
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(ViewController.dismissView))
bgView.addGestureRecognizer(tap)//change view to bgView
}
#objc func dismissView() {
self.popView.isHidden = true
bgView.backgroundColor = .white//change view to bgView
}
#IBAction func sendButton(_ sender: Any) {
self.textLabel.text = inputField.text
}
Currently this is what I have:
override func viewDidLoad() {
super.viewDidLoad()
self.setNavigationBar()
}
func setNavigationBar() {
let screenSize: CGRect = UIScreen.main.bounds
let navBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: screenSize.width, height: 44))
let navItem = UINavigationItem(title: "")
let doneItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.done, target: nil, action: #selector(done))
navItem.rightBarButtonItem = doneItem
navBar.setItems([navItem], animated: false)
self.view.addSubview(navBar)
}
#objc func done() { // remove #objc for Swift 3
The issue is that the bars title seems to be off center and the bar is very thin.
How can it be made a little thicker and with an image instead of the title?
UPDATE:
The first suggestion seemed to do nothing. Why is that? This is the first view controller.
Full code:
import UIKit
import Firebase
import FirebaseAuth
class LoginViewController: UIViewController {
#IBOutlet weak var emailField: UITextField!
#IBOutlet weak var passwordField: UITextField!
#IBOutlet weak var gifView: UIImageView!
private var ref: DatabaseReference!
override func viewDidLoad() {
let logo = UIImage(named: "q-small.png")
let imageView = UIImageView(image:logo)
self.navigationItem.titleView = imageView
gifView.loadGif(name: "truck-animation")
super.viewDidLoad()
ref = Database.database().reference()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
This is similar to Swift Navigation Bar Image Title.
Simply use this code:
let logo = UIImage(named: "logo.png")
let imageView = UIImageView(image:logo)
self.navigationItem.titleView = imageView
It seems like you are creating UINavigationBar programmatically and i am also assuming that you are creating app using storyboard.
self.navigationItem returns navigation items of your ViewController. But as per your code you must have created just UINavigationBar. So to get the title view you need UINavigationController of your ViewController.
If this is your first view of app You can add that by embedding your ViewController into Navigation Controller from Storyboard, then you don't have to add navigation bar programmatically.
After that you can use this code in viewDidLoad().
let logo = UIImage(named: "logo.png")
let imageView = UIImageView(image:logo)
self.navigationItem.titleView = imageView
I am having a problem showing the StatusBar in my UITableViewController because it does not have a background. So I want to be able to do that programatically. I have seen this in apps like Facebook and Youtube. Can anyone help me with that?
In order to add a view behind status bar, you can use below code which is written for Swift 3
You need to create an extension for it and you can use this in any view controller where it is required.
let SCREEN_WIDTH = UIScreen.main.bounds.size.width
extension UIViewController {
func addStatusBarBackgroundView(viewController: UIViewController) -> Void {
let rect = CGRect(origin: CGPoint(x: 0, y: 0), size:CGSize(width: SCREEN_WIDTH, height:20))
let view : UIView = UIView.init(frame: rect)
view.backgroundColor = UIColor.init(red: 255/255, green: 255/255, blue: 255/255, alpha: 1) //Replace value with your required background color
viewController.view?.addSubview(view)
}
}
and simply call this by writing one line in your view controller:
override func viewDidLoad() {
super.viewDidLoad()
addStatusBarBackgroundView(viewController: self)
//Your extra code
}
Happy Coding..!!
To let your UINavigationBar stretch behind you status bar, you can implement the function position(for:) on the navigation bar's delegate.
Swift3 Example
class MyViewController: UIViewController {
#IBOutlet weak var navigationBar: UINavigationBar!
override func viewDidLoad() {
super.viewDidLoad()
navigationBar.delegate = self
}
}
extension MyViewController: UINavigationBarDelegate {
func position(for bar: UIBarPositioning) -> UIBarPosition {
return .topAttached
}
}
I have some very simple code to just programmatically set a button's background image to something but when the simulator runs it shows nothing. On the storyboard it shows that the image is present however, and again nothing shows.
class ViewController: UIViewController {
#IBOutlet weak var LetUsOutlet: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
let backgroundColor = UIColor(red: 0, green: 255, blue: 247, alpha: 1)
self.view.backgroundColor = backgroundColor
let backgroundButtonImage = UIImage(named: "round rectangle button.png") as UIImage?
self.LetUsOutlet.setImage(backgroundButtonImage, for: .normal)
}
#IBAction func LetUsCreateMeal(_ sender: UIButton) {
}
#IBAction func CreateYourOwn(_ sender: UIButton) {
}
#IBAction func BrowseItems(_ sender: UIButton) {
}
#IBAction func ViewYourMeals(_ sender: UIButton) {
}
}
This is the simple code that I have so I am not sure why it isn't working properly. Below is my storyboard.
http://imgur.com/gallery/c5EWW
The first button is where I try to programatically set the background, and the other 3 are when I just set the background image property within the storyboard.
This is what happens when it runs, showing that all background images are empty.
http://imgur.com/gallery/urM25
Any help is appreciated.
EDIT: I seemed to have the image stored in the wrong place, I put it in a separate folder and not in the xassets folder.
To change button background image you should use
LetUsOutlet.setBackgroundImage(UIImage(named:"round rectangle button.png"), forState: UIControlState.Normal)
I mocked up an example - seemed to work okay for me. I suggest you check your button's constraints in IB
change this line
let backgroundButtonImage = UIImage(named: "round rectangle button.png") as UIImage?
to this line
let backgroundButtonImage = UIImage(named: "round rectangle button")
and this line
self.LetUsOutlet.setImage(backgroundButtonImage, for: .normal)
to this
self.LetUsOutlet.setBackgroundImage(backgroundButtonImage, for: .normal)
I've been having a problem with one of my Xcode projects. I'm trying to hide the navigation bar of an IOS app, but retain a white tint on the time, carrier and battery section/icons. I can only turn the tint white if I have the navigationController set to false in self.navigationController?.navigationBarHidden = false
When it is set to true, the tint turns white and there is no issue, but the navigation bar is there in color. Here is my code.
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var menuButton:UIBarButtonItem!
#IBOutlet weak var emailTxt: UITextField!
#IBOutlet weak var passwordTxt: UITextField!
#IBOutlet weak var signinBtn: UIButton!
#IBOutlet weak var signupBtn: UIButton!
var varView = Int()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
emailTxt.delegate = self
let theWidth = view.frame.size.width
let theHeight = view.frame.size.height
emailTxt.frame = CGRectMake(40, 200, theWidth-80, 30)
passwordTxt.frame = CGRectMake(40, 240, theWidth-80, 30)
signinBtn.frame = CGRectMake(theWidth-228, 340, 59, 30)
signupBtn.frame = CGRectMake(theWidth-228, 390, 59, 30)
let nav = self.navigationController?.navigationBar
nav?.barStyle = UIBarStyle.Black
nav?.tintColor = UIColor.whiteColor()
nav?.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
self.navigationController?.navigationBarHidden = true
//maparea
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true;
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
//emailTxt.resignFirstResponder()
self.view.endEditing(true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func signInBtn(sender: AnyObject) {
PFUser.logInWithUsernameInBackground(emailTxt.text!, password: passwordTxt.text!) {
(user:PFUser?, error:NSError?) -> Void in
if error == nil {
print("logIn")
self.performSegueWithIdentifier("gotoMainVCFromSigninVC", sender: self)
} else {
print("error")
}
}
}
}
In your view controller you can override preferredStatusBarStyle like this:
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return .LightContent
}
Your problem is not relate navigation bar. It relate to status bar. Your status bar is lightContent so you will see white. You just change it to Default and you will see better.
And change status bar with viewcontroller is:
Set the UIViewControllerBasedStatusBarAppearance to YES in the
.plist file.
In the viewDidLoad do a [self setNeedsStatusBarAppearanceUpdate];
Add the following method:
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return .Default
}
In your case is navigation so do it:
self.navigationController.navigationBar.barStyle = .Default
Hope this help.
answer of #adrianokw would be for one viewcontroller , if you want to do it for whole app add the following to info.plist
Status bar style
UIStatusBarStyleLightContent
View controller-based status bar appearance
NO
More info here