Swift - Toolbar done button not appearing in UIPickerView subview - ios

I'm a beginner in swift and trying to learn UIPickerView which has done button to close after the selection from the picker view. I have following code to add the tool bar and the done button to the picker as a toolbar in subview. It shows up as a blank black toolbar (attached screenshot)
let toolBar = UIToolbar()
toolBar.barStyle = .black
toolBar.sizeToFit()
let doneBtn = UIBarButtonItem.init(title: "Done", style: .plain, target: self, action: #selector(self.closePicker))
toolBar.items = [doneBtn]
toolBar.isUserInteractionEnabled = true
picker.addSubview(toolBar)
Picker is the outlet for UIPickerView in my controller. What am I doing wrong? I referred to other questions but they dont seem to solve my problem. Any suggestions?

You are wrong at this line:
picker.addSubview(toolBar)
Picker is not supposed to have any subviews - it's a comprehensive view by itself and there is no area in it to accommodate anything in addition.
Instead you need to add both the picker and the toolbar on the same view and align them next to each other
let toolBar = UIToolbar()
...configure your toolbar here...
guard let superview = picker.superview else { return }
superview.addSubview(toolBar)
toolBar.translateAutoresizingMaskIntoConstraints = false
NSLayoutConstraints.activate([
toolBar.topAnchor.constraint(equalTo: superview.topAnchor),
toolBar.leftAnchor.constraint(equalTo: superview.leftAnchor),
toolBar.rightAnchor.constraint(equalTo: superview.rightAnchor),
toolBar.bottomAnchor.constraint(equalTo: picker.topAnchor)
])

You should not add toolBar as subView of picker.
You should set the toolBar as inputAccessoryView of textField.
As I searched for this problem I got that the common way (also easiest way) to achieve what you want is Using dummy textField.
It means create a textField at the exact frame of button and hide it, when user touches the button make the textField firstResponder.
#IBAction func pickerButtonClicked(_ sender: Any) {
self.pickerViewTextField.becomeFirstResponder
}

Related

Positioning UIToolbar swift

I am adding a UIToolbar to a UIPickerview programatically and I want the toolbar to be positioned directly above the picker view, which is centered in the view controller. I have used the following to center the toolbar, but the toolbar ends up on top of (in front of) my picker view:
self.toolBar.center = self.view.center
I also tried the following, but the toolbar ends up at the top of the view controller:
self.view.addSubview(self.toolBar)
How do I go about solving this positioning issue?
below is the code used to build the toolbar:
self.toolBar.barStyle = UIBarStyle.default
self.toolBar.isTranslucent = true
self.toolBar.tintColor = UIColor.white
self.toolBar.sizeToFit()
// self.toolBar.center = self.view.center
// self.view.addSubview(self.toolBar)
let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.done, target: self, action: Selector("donePicker"))
self.toolBar.setItems([doneButton], animated: false)
self.toolBar.isUserInteractionEnabled = true
Set toolbar as an inputAccessoryView of UITextField after setting UIPickerView as inputView for textField like below,
textField.inputView = pickerView
textField.inputAccessoryView = self.toolBar
Remove adding toolBar as a subview
self.view.addSubview(self.toolBar)

How to drop an object on a bar button item?

I've got this collection view, and I want to be able to delete a cell by dragging it in the trash bar button on the right of the bar.
I don't understand how to make the button able to receive a drop,
if it was a UIView I can do
addInteraction(UIDropInteraction(..))
but since it is a button I can't act this way.
Someone can help me?
You can try creating a custom view and add interactions to it. And finally, add that to the rightBarButtonItem. Something on the lines of:
func viewDidLoadForBarButton() {
let customView = UIView() //Add your custom UIView() here
customView.sizeToFit()
let dropInteraction = UIDropInteraction(delegate: self)
customView.addInteraction(dropInteraction)
let barButton = UIBarButtonItem(customView: customView)
self.navigationItem.rightBarButtonItem = barButton
}
Let me know if it works.

Swift: Adding a toolbar and tool bar button item to a Table View

I am unable to add a tool bar to my tableView using the Xcode main.storyboard.
Thus, I tried coding it in manually in the viewDidLoad()
let logOutButton = UIBarButtonItem(title: "Log Out", style: UIBarButtonItemStyle.Bordered, target: self, action: "logOut")
var bottomBarButtonArray = [UIBarButtonItem]()
bottomBarButtonArray.append(logOutButton)
self.navigationController!.setToolbarHidden(false, animated: true)
self.navigationController!.toolbar.items = bottomBarButtonArray
May I know how do I set the logOutButton text and also how to detect if logOutButton has been pressed.
I tried logOutButton.description = "Log Out" but it does not work.
My toolbar does appear but I have no idea how to add the text for the logout button.
The solution is to use UIViewController as parent class and not UITableViewController
How to add a toolbar to a TableView in iOS
If the TableView happens to be embedded in a Navigation Controller, there is an easy solution: you can add the Toolbar straight away in the NavigationController. The checkbox 'Shows Toolbar' in the Attributes Inspector is all you need.
Details: https://stackoverflow.com/a/34832688/5897915

Cancel button is not shown in UISearchBar

I have UICollectionView. On clicking search button in UINavigationBar, I am adding the UISearchController's searchbar as titleview for UINavigationItem. For iPhone it is working properly. For iPad the cancel button is not shown. The Searchbar alone takes the entire width.
Can anyone help me out on this?. Thanks in advance.
iOS7 does not show the cancel button when added to a navigation bar.You can put searchbar in another view like this.
UISearchBar *searchBar = [UISearchBar new];
searchBar.showsCancelButton = YES;
[searchBar sizeToFit];
UIView *viewForSearchBar = [[UIView alloc]initWithFrame:searchBar.bounds];
[viewForSearchBar addSubview:searchBar];
self.navigationItem.titleView = viewForSearchBar;
I had the same problem, on iPhone the search cancel was shown well, but on iPad it didn't.
The workaround of wrapping the UISearchBar in another UIView didn't work well for me since it had different appearance and wrong width on rotation.
My solution is a simple one - use search WITHOUT cancel, and add cancel as a UIBarButtonItem.
Added rightBarButtonItem with selector will work fine for me. And adding searchBar inside view before setting to navigation title view was not showing properly.
Code:-
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.plain, target: self, action: #selector(self.dismissView))
func dismissView() {
if self.controller?.navigationController?.popViewController(animated: true) == nil {
self.controller?.dismiss(animated: true, completion: nil)
}
}
As per apple documentation setShowsCancelButton
Cancel buttons are not displayed for apps running on iPad, even when
you specify YES for the showsCancelButton parameter.
I am not sure about the alternate but this is what apple provides us.
Try this. Add a checkmark for shows cancel button.
Swift version :-
I tried the #Nikita Khandelwal method, but still it doesn't fit for ipad view. Here is the swift code, which was given as corrected answer :-
let searchBar: UISearchBar = UISearchBar()
searchBar.showCancelButton = true
searchBar.placeholder = "Search Your Job Title"
searchBar.fitToSize()
searchBar.delegate = self //do not need if you delegate searchBar
let viewForSearchBar: UIView = UIView(frame: searchBar.bounds)
viewForSearchBar.addSubview(searchBar)
self.navigationItem.titleView = viewForSearchBar
********* But There is another way to set cancel button correctly and fit for the view :-
Set search bar as the Navigation bar title view :-
let searchBar: UISearchBar = UISearchBar()
searchBar.showCancelButton = true
searchBar.placeholder = "Search Your Job Title"
searchBar.delegate = self //do not need if you delegate searchBar
self.navigationItem.titleView = searchBar
Drag and drop Bar button to the right side of the view controller & name it as Cancel.
Then connect that button to this function :-
#IBAction func iPadCancelButton(sender: AnyObject) {
UIApplication.sharedApplication().sendAction("resignFirstResponder", to:nil, from:nil, forEvent:nil)
self.dismissViewControllerAnimated(true, completion: nil)
}
For iOS 13 built with Xcode 11, I'm needing to set manually set the display value on the cancel button, depending on whether the search controller is visible

Swift - toolbar follows tableview when scrolling

In my UITableViewController, my toolbar follows my tableview when I scroll it. My code looks like this:
override func viewDidLoad() {
let toolbar: UIToolbar = UIToolbar()
let checkButton = [UIBarButtonItem(title: "Done", style: .Done, target: self, action: "checkedPress")]
toolbar.frame = CGRectMake(0, self.view.frame.size.height - 46, self.view.frame.size.width, 48)
toolbar.sizeToFit()
toolbar.setItems(checkButton, animated: true)
toolbar.backgroundColor = UIColor.redColor()
self.view.addSubview(toolbar)
}
and it looks like this when I run the app:
I want the toolbar to stick to the bottom of the view, how is this achieved?
Any suggestions would be appreciated.
The problem is that since you're using a UITableViewController, with self.view.addSubview(toolbar), you've added your toolbar as a subview of your UITableViewController's view, i.e. a UITableView. As a subview of the UITableView, the toolbar will scroll along with the table.
The solution: Use a UIView containing a UITableView instead of using a UITableViewController if you'd like to customize your view controller. That way you can add elements to the view that aren't subviews of your tableview.
You can also embed your TableViewController in a Navigation Controller and show from the storyboard or programmatically a Toolbar. This one also standard sticks to the bottom and stays on top of the Views content and you have some functionality to hide it automatically on some conditions.
You don't have to use your Navigation controller always for navigating, sometimes its a convenient way for doing stuff Xcode makes hard to use without changing your already made Views.

Resources