I made a custom UINavigationBar looks like this:[
Now I want to increase spacing between left and left bar button. I tried to do that this way using UIEdgeInsets this way:
self.navigationItem.leftBarButtonItem?.imageInsets = UIEdgeInsetsMake(0, 20, 0, 0)
Then it gets shrieked :
You can add extra spacing item before button:
let button = UIBarButtonItem(title: "<", style: .plain, target: self, action: "someSelector")
let spacing = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
spacing.width = 20;
navigationItem.leftBarButtonItems = [spacing, button]
Related
I am trying to add two bar buttons to toolbar in iOS [Cancel] & [Save] on right and left side accordingly.
I used a third bar button [Spacer] and set it to be [.flexiblewidth] Otherwise, when adding it only the left button appears [Cancel] and the [Spacer] & and [Save] which have to be next disappearing ?
the screen shot is in the link:
https://ibb.co/cZsaVV
let pickerView = UIPickerView()
override func viewDidLoad() {
pickerView.addSubview(self.setToolBar())
}
func setToolBar() -> UIToolbar {
let toolBar = UIToolbar()
toolBar.isTranslucent = true
toolBar.backgroundColor = UIColor.clear
let barButtonAttr = [NSAttributedString.Key.font : UIFont.systemFont(ofSize: 15),
NSAttributedString.Key.foregroundColor : UIColor.black]
// [Save] BarButtonItem
let saveBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.save, target: self, action: nil)
saveBarButtonItem.setTitleTextAttributes(barButtonAttr, for: .normal)
// [Cancel] BarButtonItem
let cancelBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.cancel, target: self, action: nil)
cancelBarButtonItem.setTitleTextAttributes(barButtonAttr, for: .normal)
let spacerBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace ,
target: self, action: nil)
spacerBarButtonItem.setTitleTextAttributes(barButtonAttr, for: .normal)
// add BarButtonItems to toolBar
toolBar.items = [cancelBarButtonItem,spacerBarButtonItem,saveBarButtonItem]
toolBar.sizeToFit()
return toolBar
}
func createAccessoryViewWithTarget(_ target: AnyObject, width: CGFloat) -> UIView {
// Previous button
let previousButton = UIBarButtonItem(title: "Previous", style: .plain, target: target, action: #selector(self.moveToPreviousTextField))
previousButton.tintColor = UIColor.white
//Next button
let nextButton = UIBarButtonItem(title: "Next", style: .plain, target: target, action: #selector(self.moveToNextTextField))
nextButton.tintColor = UIColor.white
// Dismiss/close/done button
let doneButton = UIBarButtonItem(title: "Done", style: .done, target: target, action: #selector(self.keyboardDoneButtonTapped))
doneButton.tintColor = UIColor.white
let keyboardToolbar: UIToolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: width, height: 44))
keyboardToolbar.barStyle = .black
let flexSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let fixedSpace = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
var itemsArray = [UIBarButtonItem]()
itemsArray.append(previousButton)
itemsArray.append(fixedSpace)
itemsArray.append(nextButton)
itemsArray.append(flexSpace)
itemsArray.append(doneButton)
keyboardToolbar.items = itemsArray
keyboardToolbar.sizeToFit()
return keyboardToolbar
}
This one is old code may be swift3 I guess. Here Im adding 3 buttons previous next and done button. flexible space and fixed space are used for spaces between buttons. Important to note here is the order that you adding your barbutton items. In your case use flexible space to place your 2 buttons on right and left end in the order of left end button, flexible space, right end button.
I've got the solution finally.
I am adding the ToolBar to the PickerView and then calling UIToolBar.SizeToFit() which is must in all cases.
the issue was I had to change the picker view size later in this case the size of subview ToolBar is not adapting with the new size coordination of pickerView. So the solution simply to call again ToolBar.SizeToFit() after any modification of parent view. here's snap of the code:
// popupView is custom UIPickerView
popupView.frame = CGRect(x:0, y:0, width:100, height:100)
// toolBar is an object of UIToolBar of the custom UIPickerView AddCurrencyPicker
(popupView as! AddCurrencyPicker).toolBar.sizeToFit()
In Swift 3, I've programmatically created a (bottom) toolbar with custom buttons separated by a flexible spacer to separate the custom buttons, pushing one ("Previous") to the left edge and the other ("Next") to the right edge of the view. But I have not been able to get the button titles ("Previous" and "Next") to display. Please advise me. Thanks. Here is my code:
First, the class declaration and a few variables:
class TextsController: UIViewController,UIGestureRecognizerDelegate {
let textWebView = WKWebView(frame: .zero)
var toolbar:UIToolbar?
Next, in the viewDidLoad:
view = textWebView
// Create (bottom) toolbar:
let frame = CGRect(x: 0, y: UIScreen.main.bounds.size.height-44, width: UIScreen.main.bounds.size.width, height: 44)
toolbar = UIToolbar(frame: frame)
toolbar?.sizeToFit()
toolbar?.isHidden = true
self.view.addSubview(toolbar!)
self.toolbar?.isTranslucent = false // Required for setting tintColor & barTintColor below
toolbar?.tintColor = UIColor(red: 0.5, green: 0.0, blue: 1.0, alpha: 1.0) //purple for toolbar items
toolbar?.barTintColor = UIColor.white //white for toolbar color
let triangleLeftButton = UIBarButtonItem(image: UIImage(named: "triangleLeft_15x20"), style: .plain, target: self, action:#selector(showPrevious))
let flexibleSpacer = UIBarButtonItem(barButtonSystemItem:.flexibleSpace , target: self, action: nil)
let triangleRightButton = UIBarButtonItem(image: UIImage(named: "triangleRight_15x20"), style: .plain, target: self, action:#selector(showNext))
triangleLeftButton.title = "Previous"
triangleRightButton.title = "Next"
var items = [UIBarButtonItem]()
items.append(triangleLeftButton)
items.append(flexibleSpacer)
items.append(triangleRightButton)
toolbar?.items = items
A custom view or image bar button item has no title. A bar button item is either a title or a custom view or an image. If you want some words to appear in a custom view or image bar button item, make those words part of the custom view or image.
Here is how I solved this.
I eliminated the two statements attempting to assign a title to each button (I do wonder why Xcode did not flag that, allowing me to assign a title to each initially, though neither title would display).
I rewrote (compare the code below with the code in the question) the UIBarButtonItems as follows to now include two text items:
// Create toolbar items (buttons and texts)
let triangleLeftButton = UIBarButtonItem(image: UIImage(named: "triangleLeft_15x20"), style: .plain, target: self, action:#selector(showPrevious))
let triangleLeftText = UIBarButtonItem(title: "Previous", style: .plain, target: nil, action: nil)
let flexibleSpacer = UIBarButtonItem(barButtonSystemItem:.flexibleSpace , target: self, action: nil)
let triangleRightText = UIBarButtonItem(title: "Next", style: .plain, target: nil, action: nil)
let triangleRightButton = UIBarButtonItem(image: UIImage(named: "triangleRight_15x20"), style: .plain, target: self, action:#selector(showNext))
Then I appended everything, including the two new text items, to the array:
var items = [UIBarButtonItem]()
items.append(triangleLeftButton)
items.append(triangleLeftText)
items.append(flexibleSpacer)
items.append(triangleRightText)
items.append(triangleRightButton)
toolbar?.items = items
This effectively places "Previous" adjacent to the left-pointing button and "Next" adjacent to the right-pointing button, with the flexible spacer affording a nice wide gap between.
i can't get my left bar button item to align vertically. Here is my current code:
override func viewDidLoad() {
super.viewDidLoad()
var image = UIImage(named: "pinImage")
image = image?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
navigationItem.rightBarButtonItem = UIBarButtonItem(image: image, style: UIBarButtonItemStyle.plain, target: self, action: #selector(handleMapView))
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "+", style: .plain, target: self, action: #selector(createTrip))
navigationItem.leftBarButtonItem?.setTitleTextAttributes([ NSFontAttributeName: UIFont(name: "Nunito", size: 48)!, NSForegroundColorAttributeName : UIColor(r: 123, g: 230, b: 200)], for: UIControlState.normal)
}
I have tried using :
navigationItem.leftBarButtonItem?.setBackgroundVerticalPositionAdjustment(4, for: .default)
but I can't get my vertical position to change. Any suggestions?
You cannot align them, because they go with your navigationBar. However, you can get that, by adding subview, and aligning your items in subview. Or another way is to use UIEdgeInsets
I'm programmatically creating a UIToolBar and its Items in swift :
var items = [UIBarButtonItem]()
let leftButton = UIBarButtonItem(image: UIImage(named: "left"), style: .Plain, target: self, action: Selector("doneClickedMaButton"))
let spacer = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil)
let rightButton = UIBarButtonItem(image: UIImage(named: "right"), style: .Plain, target: self, action: Selector("doneClickedRight"))
let searchButton = UIBarButtonItem(barButtonSystemItem: .Search, target: self, action: Selector("doneClickedSearch"))
items.append(leftButton)
items.append(spacer)
items.append(searchButton)
items.append(spacer)
items.append(rightButton)
var toolBar = UIToolbar()
toolBar.frame = CGRectMake(0, 0, self.view.frame.size.width, 50)
toolBar.translatesAutoresizingMaskIntoConstraints = false
toolBar.backgroundColor = UIColor.redColor()
toolBar.items = items
toolBar.sizeToFit()
view.addSubview(toolBar)
view.addConstraint(NSLayoutConstraint(item: toolBar, attribute: .Bottom, relatedBy: .Equal, toItem: view, attribute: .Bottom, multiplier: 1.0, constant: 0.0))
If I don't add the spacer item, then the simulator looks like this :
If I do have them in, like it says so in this code. I get this :
As you can see here, not only are the images now falling off the screen towards the left, but they're no longer in the right order.
I thought .FlexibleSpace just adds dynamic spacing between items. Am I missing something here? Someone mentioned in various posts that one could set widths to a UILabel within a UIBarButtonItem, and that could make .FlexibleSpace recognize it.
The problem is you do not have enough constraints to satisfy the auto layout engine. You are pinning the tool bar to the bottom but the width is shrinking so that it only accommodates the buttons, but you would like it to be the width of your view. Try something like:
var items = [UIBarButtonItem]()
let leftButton = UIBarButtonItem(image: UIImage(named: "left"), style: .Plain, target: self, action: Selector("doneClickedMaButton"))
let spacer = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil)
let rightButton = UIBarButtonItem(image: UIImage(named: "right"), style: .Plain, target: self, action: Selector("doneClickedRight"))
let searchButton = UIBarButtonItem(barButtonSystemItem: .Search, target: self, action: Selector("doneClickedSearch"))
items.append(leftButton)
items.append(spacer)
items.append(searchButton)
items.append(spacer)
items.append(rightButton)
var toolBar = UIToolbar()
toolBar.frame = CGRectMake(0, 0, self.view.frame.size.width, 50)
toolBar.translatesAutoresizingMaskIntoConstraints = false
toolBar.backgroundColor = UIColor.redColor()
toolBar.items = items
toolBar.sizeToFit()
view.addSubview(toolBar)
toolBar.leadingAnchor.constraintEqualToAnchor(self.view.leadingAnchor).active = true
toolBar.trailingAnchor.constraintEqualToAnchor(self.view.trailingAnchor).active = true
toolBar.bottomAnchor.constraintEqualToAnchor(self.view.bottomAnchor).active = true
I'd like to create a UIToolbar with a right-aligned done button and a left-aligned title/label.
My current solution works fine for short labels but overlaps with long ones
With autolayout I'd set the horizontal spacing to 0 and make sure my done text has higher compression resistance than the title text. But using the UIToolbar api I'm unsure how to accomplish this.
Here's the code that generates this:
lazy var pickerToolbar: UIToolbar = {
let toolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: 0, height: 33))
let leftTitle = UIBarButtonItem(title: "One and only one with lots of choices", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)
leftTitle.tintColor = UIColor.blackColor()
let space = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
let closeBtn = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: "onTapClose")
toolbar.setItems([
leftTitle,
space,
closeBtn
], animated: false)
return toolbar
}()
// ...later
textField.inputAccessoryView = pickerToolbar