UIBarButtonItem bad Edit/Done animation - ios

I've implemented an edit burron for my custom tableviewcontroller without using the default edit button. I have put a UIBarButton item in my storyboard and linked with IBOutlet in my custom class. I've implemented all these simple mechanisms for changing the button title:
class DetailTableViewController: UITableViewController {
var selectedMedicine: NSManagedObject?
var edit: Bool = false
#IBOutlet var editButton: UIBarButtonItem
#IBOutlet var nameTextField: UITextField
#IBOutlet var noteTextView: UITextView
#IBAction func enableEditing(sender: AnyObject) {
if !edit
{
println("editing")
self.edit = true
self.navigationItem.rightBarButtonItem.title = "Done"
}
else
{
self.edit = false
self.navigationItem.rightBarButtonItem.title = "Edit"
}
}
The title changes but there's something strange in the transition from Edit to Done because it's a little jerky.
I've found another one with my same problem but no one answered him.
You can look at this video for undertsand the bad animation i mean video

Instead of manually setting the bar button title and tracking state, you can use the built-in UIViewController.editButtonItem and UIViewController.editing property:
func viewWillAppear(animated:Bool) {
self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
#IBAction func enableEditing(sender: AnyObject) {
self.editing = !self.editing
}

Related

Press search button to show the searchBar from isHidden to isSelected

How can I make an if statement in swift that when I click the search button as shown in the picture below to show the search bar from isHidden to isSelected
Basically when I press the searchImage the searchBar will show from its initial isHidden to isSelected
pic
#IBAction func searchButton(_ sender: Any) {
}
#IBOutlet weak var searchField: UISearchBar!
I don't know what you need exactly but if you need to switch isHidden or isSelected you can add var to keep the search bar state like:
var isSearchFieldHidden: Bool = false{
didSet{
self.searchField.isHidden = isSearchFieldHidden
}
}
#IBAction func searchButton(_ sender: Any) {
isSearchFieldHidden.toggle()
}
#IBOutlet weak var searchField: UISearchBar!
Try to use
Button(action: {
isHidden.toggle()
}) {
Image("search_icon")
}
!isHidden{
// any view: Button, TextField etc.
your_searchBarView()
}
and read: https://developer.apple.com/documentation/swiftui/text/hidden()#declaration

Swift - UIToolbar item selected state

I have this toolbar in my navigation controller. Now what I am trying to do is when the user selects an item (UIBarButtonItem) in my toolbar, have that item highlighted with a background colour until either the user deselects the item or selects another item. How would I do this?
Here are my selector methods for each item of the toolbar, I connected them via storyboard:
#IBAction func addText(sender: AnyObject) {
annotationSelected = 3
}
#IBAction func drawCircle(sender: AnyObject) {
annotationSelected = 1
}
#IBAction func drawRectangle(sender: AnyObject) {
annotationSelected = 2
}
#IBAction func drawStamp(sender: AnyObject) {
annotationSelected = 4
}
This is all I have done. Here is a screenshot of my toolbar:
Here is what I got:
#IBOutlet var textToolButton: UIBarButtonItem!
#IBOutlet var circleToolButton: UIBarButtonItem!
#IBOutlet var rectangleToolButton: UIBarButtonItem!
#IBOutlet var stampToolButton: UIBarButtonItem!
then
textToolButton.target = self
textToolButton.style = .Done
textToolButton.action = #selector(ViewController.barButtonPressed)
let selectedBackgroundColor = UIImage(color: .redColor())
textToolButton.setBackgroundImage(selectedBackgroundColor, forState: UIControlState.Highlighted, style: .Done, barMetrics: UIBarMetrics.Default)
and then the method
func barButtonPressed(sender: UIBarButtonItem) {
print(sender)
annotationSelected = sender.tag
}
background is still not changing color
I find a same question. May be can help you.
custom-pressed-uibarbuttonitem-backgrounds
I find a easy method to do. You can dray a button to the toolBar,and you will see like this.
And you should change the button's type and Image.
storyboard screenshot
then you should link the button to your viewController.
#IBOutlet weak var textToolButton: UIButton!
and you can do.
let selectedBackgroundColor = UIImage(color: .redColor())
textToolButton.setBackgroundImage(selectedBackgroundColor, forState: .Highlighted)
May be I can help you.
The cleanest way you could do it, is create an overall function where you pass in the button that's been selected. Something like this:
var allButtons = [button1, button2, button3, button4]
func resetTabBar (buttonSelected:UIButton) {
for button in allButtons {
if button == buttonSelected {
button.backgroundColor = "Black"
}
else {
button.backgroundColor = "Blue"
}
}
}
And then in your functions you've created, just pass in the sender like so:
#IBAction func addText(sender: AnyObject) {
resetTabBar(sender)
}
Note: This is assuming you have outlets for all of your buttons. If you don't, add them.

How to change label to original text after it's been changed with a function in Swift?

I have a button that changes a text view when it's pressed, I want it to do the changed text to be switched into the original text when the button is pressed again.
My Code:
//An IBOutlet for the textview
#IBOutlet weak var changingText: UITextView!
//The button that changed the text
#IBAction func viewChangedText(sender: AnyObject) {
changingText.text = "Changed Text"
}
Now I want the text changed back when the button is pressed again.
Note: The original text is in in the storyboard, and if possible. could you help me change the button's text to "View" when the original text is there and change the button's text to "Hide" when the changed text is there.
var didChange = false
#IBOutlet weak var changingText: UITextView!
//The button that changed the text
#IBAction func viewChangedText(sender: AnyObject) {
didChange = !didChange
if didChange {
changingText.text = "Changed Text"
}else{
changingText.text = "Original text"
}
}
#nathanwhy has a nice idea with the switch-bool. Building upon that idea you could have something like this:
#IBOutlet weak var changingTextView: UITextView!
var showOriginalText : Bool {
didSet {
changingTextView.text = showOriginalText ? "original text" : "other text"
}
}
override func viewDidLoad() {
super.viewDidLoad()
// or wherever else it makes sense
showOriginalText = true
}
#IBAction func changeButtonTapped(sender: UIButton) {
showOriginalText = !showOriginalText
}

Changing UITextfield from Non-Editable to Editable - Swift

I am trying to have two non-editable UITextField display name and age. I have an Edit UIBarButtonItem in my Navigation Bar that I want to be able to trigger the UITextField to be editable when that button is pressed.
In my Interface Builder, I have the User Interaction Enabled option unchecked for the two UITextFields. Do I need to add ageText.UserInteractionEnabled = true? I'm at a loss here.
class UserProfileVC: UIViewController,
UITextFieldDelegate, UINavigationControllerDelegate {
#IBOutlet weak var infoBorder: UILabel!
#IBOutlet weak var nameText: UITextField!
#IBOutlet weak var ageText: UITextField!
var textFields:[UITextField] = []
#IBAction func editButton(sender: UIBarButtonItem) {
nameText.becomeFirstResponder()
ageText.becomeFirstResponder()
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
var currentTextField = textFields[0]
if (currentTextField == textField) {
currentTextField = textFields[1]
currentTextField.becomeFirstResponder()
} else {
currentTextField.resignFirstResponder()
}
return true
}
}
Yes, you want to do
nameText.userInteractionEnabled = true
ageText.userInteractionEnabled = true
and possibly
nameText.becomeFirstResponder()
when the edit button gets pressed the first time. You will probably also want to change the "Edit" button do a "Done" button. When the user presses the done button, you'll want to make sure you disable user interaction and resign first responder for both text fields.
You should have a Bool Variable. Set it to 'false' then when the button is pressed you toggle it to 'true'. Depending on its state just run to different methods which essentially allows you to edit or not.
Something like this:
var editTextFieldToggle: Bool = false
#IBoutlet var textFieldToggle: UIButton!
#IBAction func textFieldToggle_Action(sender: UIButton){
editTextFieldToggle = !editTextFieldToggle //switches button ON/OFF
if editTextFieldToggle == true {
textFieldActive()
} else {
textFieldDeactive()
}
}
textFieldActive(){
//Turn things ON
nameText.enabled == true
ageText.enabled == true
}
textFieldDeactive(){ //Add anything else
//Turn things OFF
nameText.enabled == false
ageText.enabled == false
}

How to disable button touch at the start in Swift

I have this button:
#IBAction func touch(sender: AnyObject) {
println("Hello, world!")
}
and another:
#IBAction func toggle(sender: AnyObject) {
//code to enable touch of button "touch"
I want to disable user touch of "touch" at the start of the app, and enable touch after tapping "toggle", how to realize it by code and in storyboard?
You need #IBOutlets to access the buttons at runtime. In InterfaceBuilder ctrl-drag the button to the top of your custom class file, choose Outlet and give it a descriptive name. It will look like this:
class MyViewController: UIViewController {
#IBOutlet weak var touch: UIButton!
#IBOutlet weak var toggle: UIButton!
// Disable in `viewDidLoad`:
override func viewDidLoad() {
super.viewDidLoad()
self.touch.enabled = false
}
#IBAction func toggle(sender: AnyObject) {
//code to enable touch of button "touch"
self.touch.enabled = true
}
}
Swift 3 syntax
button.isEnabled = true
An update with swift 2 and IOS 10 is self.touch.isEnabled = false.
They have gotten rid of enabled and replaced it with isEnabled.
You can disable the button in the storyboard - uncheck the enabled checkbox.
To enable it in code you should create an outlet and then set the enabled property to true:
button.enabled = true
Solution 1
myButton.isEnabled = false;
Solution 2 //Desperate way to disable button by disabling the user interaction.
myButton.isUserInteractionEnabled = false;

Resources