Done button on toolbar sometimes disappears and sometimes turns white - ios

I have a toolbar for above the keyboard in an iOS swift project. When I click thorugh my app and go to certain pages before going to the one with the toolbar the done button is gone. If you click another textfield on the page it comes back until you go back to the pages of the app that mess it up. If I remove the flexible space button, it never disappears. I have checked the frame size of the toolbar and it never seems to change. The button also sometimes turns white. Not sure how this is happening if anyone has any ideas? This congifureToolbar() is called in viewdidload. I also tried calling it in viewdidappear and tried calling layoutSubviews. inputToolbar is just a UIToolbar(). I tried with custom xib toolbar, custom xib view, third party library toolbar, it never makes a difference.
func configureToolBar() {
inputToolbar.barTintColor = UIColor.groupTableViewBackground
inputToolbar.barStyle = .default
inputToolbar.isTranslucent = false
inputToolbar.sizeToFit()
inputToolbar.tintColor = UIColor(red: 0/255, green: 102/255, blue: 252/255, alpha: 1.0)
inputToolbar.setShadowImage(AppColors.shared.tableViewSeparatorColor.as1ptImage(), forToolbarPosition: .any)
let doneButton = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(dismissKeyboard))
let flexibleSpaceButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let fixedSpaceButton = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
doneButton.tintColor = UIColor(red: 0/255, green: 102/255, blue: 252/255, alpha: 1.0)
doneButton.setTitleTextAttributes([NSAttributedStringKey.foregroundColor : UIColor(red: 0/255, green: 102/255, blue: 252/255, alpha: 1.0)], for: .normal)
let nextButton = UIBarButtonItem(image: imageLiteral(resourceName: "toolbar-down"), style: .plain, target: self, action: #selector(moveToNextInput))
nextButton.tintColor = AppColors.shared.blackColor
nextButton.width = 50.0
let previousButton = UIBarButtonItem(image: imageLiteral(resourceName: "toolbar-up"), style: .plain, target: self, action: #selector(moveToPreviousInput))
previousButton.tintColor = AppColors.shared.blackColor
inputToolbar.setItems([fixedSpaceButton, previousButton, fixedSpaceButton, nextButton, fixedSpaceButton, doneButton], animated: false)
inputToolbar.isUserInteractionEnabled = true
}
If instead of a flexible space button, I put a fixed space one, I can see that the done button is getting pushed off the screen, because I can get it to be partially cut off.

Related

Swift - UIToolbar with Done button for UIPickerView disappear on landscape

I made an UIPickerView as an input for a UITextField and implement a UIToolbar with a "Done" button. It works perfectly but when I switch to landscape mode on my iPhone the UIToolbar disappear. Here's my code:
override func viewDidLoad() {
super.viewDidLoad()
thePicker.delegate = self
let toolBar = UIToolbar()
toolBar.barStyle = UIBarStyle.default
toolBar.isTranslucent = true
toolBar.tintColor = UIColor(red: 76/255, green: 100/255, blue: 217/255, alpha: 1)
toolBar.sizeToFit()
let doneButton = UIBarButtonItem(title: "Aceptar", style: UIBarButtonItemStyle.plain, target: self, action: #selector(doneClick))
toolBar.setItems([doneButton], animated: false)
toolBar.isUserInteractionEnabled = true
testTextField.inputView = thePicker
testTextField.inputAccessoryView = toolBar
}
Faced the same problem when I add UIPickerView programmatically and add UIToolBar for the PickerView. Just need to add [.flexibleWidth,.flexibleHeight] for the UIPickerView
self.thePicker.autoresizingMask = [.flexibleWidth,.flexibleHeight]

CNContactViewController button action

I am using CNContactViewController. But, I want to add a new button. How to I do
If you can not add a new button then how can I get the action of the Call button?
can you try like this but i have used for new contact and you can customize with you code.
func CreateNewContact() {
let contact = CNMutableContact()
let unkvc = CNContactViewController(forNewContact: contact)
unkvc.delegate = self
unkvc.allowsEditing = true
unkvc.allowsActions = true
unkvc.title = "Create New Contact"
self.navigationController?.isNavigationBarHidden = false
self.navigationController?.navigationBar.backgroundColor = UIColor(red: 234.0/255.0, green: 34.0/255.0, blue: 39.0/255.0, alpha: 1.0)
self.navigationController?.navigationBar.tintColor = UIColor(red: 255.0/255.0, green: 255.0/255.0, blue: 255.0/255.0, alpha: 1.0)
Application.Delegate.setStatusBarBackgroundColor(color: UIColor(red: 198/255.0, green: 6/255.0, blue: 39.0/255.0, alpha: 1.0))
self.navigationController?.navigationItem.hidesBackButton = true
let rightButton: UIBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.done, target: self, action: #selector(ShowInfotoshare.doneButtonClicked(_:)))
self.navigationController?.navigationItem.rightBarButtonItem = rightButton
self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white]
self.navigationController?.pushViewController(unkvc, animated: false)
}
func doneButtonClicked(_ button:UIBarButtonItem!){
print("Done clicked")
}
If you have any query then show me your code so i will help you.
AFAIK, the row of buttons under the name (lets call them "methods of communications") cannot be modified. You can't hide the row, or individual buttons, you can't add anything into the row.
Also, the buttons active/inactive state is controlled by the current contact information (that is displayed below it).
Finally, note that the simulator, because it doesn't present a phone calling service nor include Mail.app, the buttons status will be pretty different from a real device.

Why are programmatically created (bottom) toolbar button titles not displaying?

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.

inputAccessoryView's UIToolbar turns black when rotating in iOS

Here is my code:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let keyBoardToolBar = UIToolbar(frame: CGRectMake(0, 0, self.view.frame.size.width, 44))
keyBoardToolBar.barStyle = .Default
let flexSpaceKeyboardBarButtonItem = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil)
let doneKeyboardBarButtonItem = UIBarButtonItem(title: "Done", style: .Done, target: self, action: nil)
let wordKeyboardBarButtonItem = UIBarButtonItem(title: "Button 1", style: .Plain, target: self, action: nil)
var barItems: [UIBarButtonItem] = []
barItems.append(wordKeyboardBarButtonItem)
barItems.append(flexSpaceKeyboardBarButtonItem)
barItems.append(doneKeyboardBarButtonItem)
keyBoardToolBar.setItems(barItems, animated: true)
self.myTV.inputAccessoryView = keyBoardToolBar
}
And when I am turning the device, the UIToolBar become black: (click to see this GIF again)
So is there anyway to fix it? Thanks!
BTW: In Simulator I cannot see UIToolBar turning black.
Fixed by adding: (still unclear about the reason causing it though)
keyBoardToolBar.isTranslucent = false
keyBoardToolBar.barTintColor = UIColor(colorLiteralRed: (247/255), green: (247/255), blue: (247/255), alpha: 1)
(UIColor(colorLiteralRed: (247/255), green: (247/255), blue: (247/255), alpha: 1) is the default background color of the UIToolBar from here)

UIBarButtonItem not clickable in UIToolBar

I have a UIPickerView and I am adding a UIToolBar with 2 UIBarButtonItems to it.
var toolBar = UIToolbar()
toolBar.frame.origin.y = -40
toolBar.barStyle = UIBarStyle.Default
toolBar.translucent = true
toolBar.tintColor = UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1)
toolBar.sizeToFit()
var doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Bordered, target: self, action: "donePicker")
var spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
var cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Bordered, target: self, action: "canclePicker")
toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
toolBar.userInteractionEnabled = true
pickerView.addSubview(toolBar)
pickerView.bringSubviewToFront(toolBar)
view.bringSubviewToFront(toolBar)
The problem is, that when I try to click on the the UIBarButtonItem, it doesn't fire, it doesn't even recognize it, it just clicks the cell beneath it.
Can you try this codes.
var textField = UITextField(frame: CGRectMake(20, 50, view.width - 40, 30))
textField.backgroundColor = UIColor.redColor()
view.addSubview(textField)
var pickerView = UIPickerView(frame: CGRectMake(0, 200, view.width, 300))
pickerView.backgroundColor = UIColor.whiteColor()
pickerView.showsSelectionIndicator = true
var toolBar = UIToolbar()
toolBar.barStyle = UIBarStyle.Default
toolBar.translucent = true
toolBar.tintColor = UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1)
toolBar.sizeToFit()
var doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Bordered, target: self, action: "donePicker")
var spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
var cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Bordered, target: self, action: "canclePicker")
toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
toolBar.userInteractionEnabled = true
textField.inputView = pickerView
textField.inputAccessoryView = toolBar
I believe that you need to raise the picker and toolbar using the standard UIResponder mechanism or cheat by using a hidden UITextField to get the same result.
See my updated answer to your original question:
Add buttons to UIPickerView - Swift 1.2

Resources