I made a function to display navigation items and bar buttons, however for some reason it doesn't seem to work and it doesn't display anything
override func viewDidLoad() {
configureViewComponents()
}
func configureViewComponents(){
view.backgroundColor = UIColor.init(red: 0/255, green: 0/255, blue: 201/255, alpha: 1)
navigationItem.title = "Login"
navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "arrowshape.turn.up.left"), style: .plain, target: self, action: #selector(handleSignOut))
navigationItem.leftBarButtonItem?.tintColor = .white
If trying to render SFSymbol Icon, use the UIImage(systemName:) initializer:
navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(systemName: "arrowshape.turn.up.left"), style: .plain, target: self, action: #selector(handleSignOut))
What colour is the background of your navigation bar because you are making the left button item have a tint of white. If your navigation bar is also white I do not believe this will be displayed, as the colours are just the same.
Therefore, I suggest trying to change the tint of the button and see if that makes any difference.
Related
I have this left UIBarButtonItem that performs like a switch, so I would like to make it gray when its off and the normal tint color when its on but I can't figure out how
Here is the code used to assign the buttons:
doneHomeworksButton = UIBarButtonItem(image: doneHomeworksButtonImage, style: .plain, target: self, action: #selector(doneHomeworksClicked))
addButton = UIBarButtonItem(image: plusButtonImage, style: .plain, target: self, action: #selector(plusButtonClicked))
self.navigationItem.rightBarButtonItem = addButton
self.navigationItem.leftBarButtonItem = doneHomeworksButton
This is the simulator (I want to change the left button)
This is the way, I use text instead of icon. you can user FM symbols of fontawesome or any other. And also you can change the image when button click
class ViewController: UIViewController{
var btnTick:UIBarButtonItem?
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
btnTick = UIBarButtonItem(title: "2", style: .plain, target: self, action: #selector(didClickedBtn(_ :)))
btnTick?.tintColor = .blue
navigationItem.leftBarButtonItem = btnTick
}
#objc func didClickedBtn(_ sender : UIBarButtonItem){
sender.tintColor = .red
view.layoutIfNeeded()
}
}
I am trying to set up a navigation button to appear red and semibold. I managed to changed the color but I'm having trouble changing it to semibold:
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Sign Up", style: .plain, target: self, action: #selector(signIn))
navigationItem.rightBarButtonItem?.tintColor = .red
UIBarItem.appearance().setTitleTextAttributes(
[
NSAttributedString.Key.font: UIFont.systemFont(ofSize: 12, weight: .semibold)
],
for: .normal)
I tried rightBarButtonItem.appearance first but that doesn't seem to be an option.
I'm working with Swift 4.2.
You can set the style property to .done to make the UIBarButtonItem bold. This will work until Apple changes how the done button looks in a newer version of iOS.
navigationItem.rightBarButtonItem?.style = .done
Well to have fully customized bar button appearance, you can use bar button items with custom view just like this
let doneButton: UIButton = UIButton (type: UIButton.ButtonType.custom)
doneButton.setTitle("Done", for: .normal)
doneButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
doneButton.setTitleColor(.blue, for: .normal)
doneButton.addTarget(self, action: #selector(self.doneBarButtonTapped(sender:)), for: UIControl.Event.touchUpInside)
doneButton.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
let barButton = UIBarButtonItem(customView: doneButton)
navigationItem.rightBarButtonItem = barButton
I created a textview like below and I add keyboard button for users can change the font. It is working but it changes whole text's font but I want to change fonts only after button clicking. Can you help about ?
func setKeyboard() { // this is for keyboard button
let bar = UIToolbar()
let flex = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: self, action: nil)
let font = UIBarButtonItem(image: UIImage(named: "icons8-image-80"), style: .plain, target: self, action: #selector(fontTapped))
bar.barTintColor = UIColor(displayP3Red: 30/255, green: 30/255, blue: 30/255, alpha: 1)
bar.sizeToFit()
writePost.inputAccessoryView = bar
}
// and this one is fontTapped function
#objc func fontTapped() {
if fontTag == 0 {
let attributedText = NSMutableAttributedString(string: writePost.text, attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 12)])
writePost.attributedText = attributedText
fontTag = 1
} else {
writePost.font = UIFont(name: "AvenirNext-DemiBoldItalic", size: 16)
fontTag = 0
}
}
To change the font / style for what the user types from now on, change the typingAttributes of the text view.
https://developer.apple.com/documentation/uikit/uitextview/1618629-typingattributes
You will be using the attributedText property of the UITextField like so:
https://medium.com/swift-india/multiple-font-style-for-uilable-text-using-nsattributed-string-3f121036a533
And probably build your attributed string in shouldChangeCharactersInRange() of the UITextFieldDelegate, in order to change the font of the incoming characters.
How shouldChangeCharactersInRange works in Swift?
I am trying to programatically show and add toolbar buttons to a toolbar in a UITableViewController embedded in a UINavigationController. The toolbar appears but no buttons appear. Here is my code:
var items = [UIBarButtonItem]()
items.append(UIBarButtonItem.init(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil))
items.append(UIBarButtonItem(title: "Clear", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("btnClearAction")))
self.navigationController!.setToolbarHidden(false, animated: true)
self.navigationController!.setToolbarItems(items, animated: false)
While migrating my app to swift, I just found out I had the same issue. Only I'm trying to add it to a left menu controller which holds a table view. Shouldn't be all that different?
I just now got it solved by creating the toolbar programmatically.
Here's what I did:
Under the class
class LeftViewController: ViewController, UITableViewDataSource, UITableViewDelegate {
var tableView: UITableView!
var toolBar: UIToolbar!
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor(red: 45/255, green: 45/255, blue: 45/255, alpha: 1.0)
self.tableView = UITableView(frame: self.view.bounds, style: .Plain)
self.tableView.delegate = self
self.tableView.dataSource = self
self.view.addSubview(self.tableView)
//UPDATE #3
self.tableView.frame = CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height-20)
self.edgesForExtendedLayout = .None
navigationController?.navigationBar.barTintColor = UIColor.blackColor()
navigationController?.navigationBarHidden = true
let Button1 = UIBarButtonItem(title: "Button1", style: .Plain, target: self, action: "your action")
let Button2 = UIBarButtonItem(title: "Button2", style: .Plain, target: self, action: "your action")
let Button3 = UIBarButtonItem(title: "Button3", style: .Plain, target: self, action: "your action")
let Button4 = UIBarButtonItem(title: "Button4", style: .Plain, target: self, action: "your action")
//Flexiable Space
let flexiableItem = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil)
//UPDATE #2
toolbarItems = [Button1, Button2, flexiableItem, Button3, Button4]
}
Obviously style your buttons, view, cells etc..however you want.
I did leave a lot out of what I posted because it didn't pertain to the topic.
Hope this helps.
UPDATE:
Awesome. One fix brings on another problem.
My toolbar vanishes when rotating the device. Rotate back and the buttons are gone. Could just be something in my project but yah.
UPDATE #2:
Answer updated with working code. Removed the toolBar I made and found a solution which works. Even with rotation.
UPDATE #3:
Sets the table view 20px creating a "status bar" and reduces the bottom of the table view by 20px so it fits on top of the toolbar.
When I create my navigation bar, I can place in left item, right item and in the title view, three buttons but only the left and right item have an animation and I can't change the color of the title button.
The creation of the buttons in the navigation bar :
self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "VOYAGE", style: UIBarButtonItemStyle.Plain, target: self, action: "goToPage1:")
var buttonCenter = UIButton()
buttonCenter.setTitle("PHOTOS", forState: .Normal)
buttonCenter.setTitleColor(UIColor.whiteColor(), forState: .Normal)
buttonCenter.addTarget(self, action: "goToPage2:", forControlEvents: .TouchUpInside)
self.navigationItem.titleView = buttonCenter
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "SIGHTINGS", style: UIBarButtonItemStyle.Plain, target: self, action: "goToPage3:")
The changement of the color when in touche on button:
Function goToPage1 :
self.navigationItem.leftBarButtonItem!.tintColor = UIColor(red: 174/255, green: 211/255, blue: 43/255, alpha: 1)
self.navigationItem.titleView!.tintColor = UIColor.whiteColor()
self.navigationItem.rightBarButtonItem!.tintColor = UIColor.whiteColor()
Function goToPage2 :
self.navigationItem.leftBarButtonItem!.tintColor = UIColor.whiteColor()
self.navigationItem.titleView!.tintColor = UIColor(red: 174/255, green: 211/255, blue: 43/255, alpha: 1)
self.navigationItem.rightBarButtonItem!.tintColor = UIColor.whiteColor()
Function goToPage3 :
self.navigationItem.leftBarButtonItem!.tintColor = UIColor.whiteColor()
self.navigationItem.titleView!.tintColor = UIColor.whiteColor()
self.navigationItem.rightBarButtonItem!.tintColor = UIColor(red: 174/255, green: 211/255, blue: 43/255, alpha: 1)
Have you an idea for the selection animation of the button?
Thanks for your help.
You can try this steps :
in the main.storyboard, for each view, you can change in the simulated metrics section the value of top bar to translucent navigation bar
add in the navigation item created your buttons
link the buttons from main.storyboard to your file and change the color with self.yourButton.setTitleColor(UIColor.whiteColor(), forState: .Normal)