How to make a toolbar be attached to bottom iOS Swift - swift

I have a ploblem with tab bar and navigation bar. There's a toolbar I need to display on top of the tabbar, but attached to the very bottom.
For now I'm getting this:
My code is:
private func showToolBar() {
tabBarController?.tabBar.isHidden = true
navigationController?.setToolbarHidden(false, animated: true)
let archiveButton = UIBarButtonItem(image: #imageLiteral(resourceName: "deactivateIcons"), style: .plain, target: self, action: #selector(archive))
let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
let refreshButton = UIBarButtonItem(image: #imageLiteral(resourceName: "changeStatusIcons"), style: .plain, target: self, action: #selector(refresh))
archiveButton.tintColor = Colors.Purple
refreshButton.tintColor = Colors.Purple
setToolbarItems([archiveButton, flexibleSpace, refreshButton], animated: true)
navigationController?.toolbar.setShadowImage(UIImage(), forToolbarPosition: .bottom)
}
So the toolbar appears exactly on top of the tabbar. How to solve this? Thanks in advance!

Try this:
put that code in the last line of your showToolBar() function
let screenSize: CGRect = UIScreen.main.bounds
navigationController?.toolbar.frame = CGRect(x: 0, y: screenSize.height - tabBarHeight - 50, width: screenSize.width, height: 50)

Related

Issue making toolbar with custom height | Swift 5

Essentially I have the following toolbar that is currently set to contain two buttons on both the left and right side:
//Select All Toolbar
self.navigationController?.isToolbarHidden = false
self.navigationController?.toolbar.barTintColor = UIColor(named: "tabBarColor")
//self.navigationController?.toolbar.barTintColor = UIColor.white
self.navigationController?.toolbar.sizeToFit() // without this line it doesn't work
self.navigationController?.toolbar.tintColor = UIColor(named:"toolBarRedColor")
var items = [UIBarButtonItem]()
let selectbutton = UIBarButtonItem(title: "Left Button", style: .plain, target: self, action: #selector(printMe))
let unselectbutton = UIBarButtonItem(title: "Right Button", style: .plain, target: self, action: #selector(printMe))
items.append(selectbutton)
items.append( UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil))
items.append(unselectbutton)
self.toolbarItems = items
I would like to expand the height of the tool bar and have tried doing the following:
class CustomToolbar: UIToolbar {
override func sizeThatFits(_ size: CGSize) -> CGSize {
var newSize: CGSize = super.sizeThatFits(size)
newSize.height = 80 // there to set your toolbar height
return newSize
}
}
What is being done incorrectly? The height is not being adjusted.
Try this:
let navBarSize = CGSize(width: navigationController!.navigationBar.bounds.width, height: 200)
override func viewDidLayoutSubviews() {
navigationController?.navigationBar.frame = CGRect(x: 0, y: 0, width: navBarSize.width, height: navBarSize.height)
}
Hope this helps :)

Do I need to add constraints to my UIToolBar if I'm adding it as a subview to my UIPickerView?

I'm trying to add a simple "Done" button to my UIPickerView programmatically, without using storyboards.
When I try to add toolBar as a subview of the UIPickerView, the toolbar doesn't even show up and I get some constraint related errors.
Any idea on how I can add the button to the PickerView?
Here is a snippet of my code:
var timerImage = UIButton()
var timer = Timer()
var timerDisplayed = 0
let image1 = UIImage(named: "stopwatch")
let timePicker = UIPickerView()
let timeSelect : [String] = ["300","240","180","120","90","60","45","30","15"]
let toolBar = UIToolbar()
let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(ThirdViewController.dismissKeyboard))
func pickerViewConstraints(){
timePicker.anchor(top: nil, leading: view.safeAreaLayoutGuide.leadingAnchor, bottom: view.safeAreaLayoutGuide.bottomAnchor, trailing: view.safeAreaLayoutGuide.trailingAnchor)
}
#objc func timeClock(){
toolBar.setItems([doneButton], animated: true)
toolBar.sizeToFit()
toolBar.isTranslucent = false
toolBar.isUserInteractionEnabled = true
toolBar.barStyle = .default
view.addSubview(timePicker)
timePicker.addSubview(toolBar)
pickerViewConstraints()
timePicker.backgroundColor = .white
DispatchQueue.main.async {
self.timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.Action), userInfo: nil, repeats: true)
self.timerImage.setImage(nil, for: .normal)
}
}
No, you don't need to add constraints. It can be like :
let space = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let doneButton = UIBarButtonItem(title: buttonTitle, style: .done, target: self, action: #selector(doneButtonAction))
let cancelButton = UIBarButtonItem(title: cancelTitle, style: .plain, target: self, action: #selector(cancelButtonAction))
let barAccessory = UIToolbar(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: 44))
barAccessory.barStyle = .default
barAccessory.isTranslucent = true
barAccessory.barTintColor = .blue
barAccessory.setItems([cancelButton, space, doneButton], animated: false)
picker.addSubview(barAccessory)
self.view.bringSubviewToFront(picker)
Hope it helps...
You need to set a delegate of type UIPickerViewDelegate to your picker view. In this delegate, implement the delegate method pickerView:viewForRow:forComponent:reusingView
to provide your custom view for the picker item.
For the custom view you provide, you can design it however you want, including adding the button.
It looks like it's just a few minor issues here. Here's how I've done it prior:
override func viewDidLoad() {
super.viewDidLoad()
createToolbar()
}
// Toolbar for "done"
func createToolbar() {
let toolBar = UIToolbar()
toolBar.sizeToFit()
let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(PageOneViewController.dismissKeyboard))
toolBar.setItems([doneButton], animated: false)
toolBar.isUserInteractionEnabled = true
// Makes toolbar apply to text fields
educationText.inputAccessoryView = toolBar
politicalText.inputAccessoryView = toolBar
drinkingText.inputAccessoryView = toolBar
heightText.inputAccessoryView = toolBar
schoolInput.inputAccessoryView = toolBar
}
#objc func dismissKeyboard() {
view.endEditing(true)
}
It seems like you haven't technically "called" the toolbar / done button, unless I'm just not seeing that part of the code. Also I've nested the "done" code inside of the function.
If you're using text fields then you should be able to follow to "educationText.inputAccessoryView = toolBar" format to apply all of the code above to each text field (education, politics, drinking, etc). I hope this helps! Good luck.

How can i move a button in a UINavigation bar to the right

im trying to move the right button in my navigation bar to the right to align it with buttons on a table view here is what is currently looks like
i made a button like this:
let allButton = UIButton(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
allButton.layer.borderColor = Constants.AppColor.cgColor
allButton.layer.borderWidth = 1
allButton.layer.backgroundColor = UIColor.white.cgColor
allButton.addTarget(self, action: #selector(selectAllContacts), for: .touchUpInside)
allButton.layer.cornerRadius = 10
and im adding it to my nav bar like this:
navigationItem.rightBarButtonItems = [UIBarButtonItem(customView: allButton), importButton]
I have tried adding image insets, content insets and it doesn't work.
Any idea on what else i can try?
Will this do?
navigationItem.rightBarButtonItem = UIBarButtonItem(
title: "Menu >",
style: .plain,
target: self,
action: #selector( rightNavTap )
)
I was able to solve this by adding
let negativeSpacer:UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.fixedSpace, target: nil, action: nil)
negativeSpacer.width = -10
and appending that to my array of buttons
navigationItem.rightBarButtonItems = [negativeSpacer, UIBarButtonItem(customView: allButton), importButton]
If anyone finds a better solution let me know.

How can I add UIToolbar to UIPickerView without using UITextField?

I have used UIPickerView without UITextField. After user tap on button, UIPickerView will display.
Anyway, I have no idea how to dismiss UIPickerView. I have tried to set inputAccessoryView with UIToolbar but the complier said it's get-only property.
My code:
let toolbar = UIToolbar()
toolbar.barStyle = UIBarStyle.default
toolbar.isTranslucent = true
toolbar.sizeToFit()
let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.done, target: self, action: #selector(RegisterViewController.pickerDoneButton))
let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: self, action: nil)
let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.plain, target: self, action: #selector(RegisterViewController.pickerCancelButton))
toolbar.setItems([cancelButton, spaceButton, doneButton], animated: true)
toolbar.isUserInteractionEnabled = true
pickerView.inputAccessoryView = toolbar
Error: Cannot assign to property: 'inputAccessoryView' is a get-only property
Try to add subview to the UIPickerView
It seems like UIPickerView is not letting its children receive touch events. If you want to add toolbar to pickerview without textfield you can create a top level container UIView to hold both toolbar and picker view like this:
let picker = UIView(frame: CGRect(x: 0, y: view.frame.height - 260, width: view.frame.width, height: 260))
// Toolbar
let btnDone = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(self.monthdoneButtonAction))
let spaceButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let cancelButton = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(self.cancelClick))
let barAccessory = UIToolbar(frame: CGRect(x: 0, y: 0, width: picker.frame.width, height: 44))
barAccessory.barStyle = .default
barAccessory.isTranslucent = false
barAccessory.items = [cancelButton, spaceButton, btnDone]
picker.addSubview(barAccessory)
// Month UIPIckerView
monthPicker = UIPickerView(frame: CGRect(x: 0, y: barAccessory.frame.height, width: view.frame.width, height: picker.frame.height-barAccessory.frame.height))
monthPicker.delegate = self
monthPicker.dataSource = self
monthpickerData = ["January","February","March","April","May","June","July","August","September","October","November","December"]
monthPicker.backgroundColor = UIColor.white
picker.addSubview(monthPicker)

How to show and add toolbar buttons to UITableViewController with 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.