Sarath Kumar
Sarath Kumar

Reputation: 57

Barbuttons are moving off the position

Everything is fine until I present a uialertcontroller. When i present it,the barbuttons are moving down from thier position.

Before presenting,everything was fine with barbuttons

When i present an alert,you can see the UIBarButtonItems are off their position

Code for creating barbuttonitems::

let editBtn:UIBarButtonItem = UIBarButtonItem(image: UIImage(named: IMAGE_EDIT_ICON), style: .plain, target: self, action: #selector(handleEditAction))
self.navigationItem.rightBarButtonItem = editBtn

Presenting UIAlertControlelr as follows:

let alertController = UIAlertController(title: "Reset Password", message: "Please enter current password and create new password", preferredStyle: .alert)
   let okAction =  UIAlertAction(title: "Confirm", style: .default, handler: { [weak alertController] (_) in
   })


let cancelAction = UIAlertAction(title: "Cancel", style: .default) { (_) in }


alertController.addAction(cancelAction)
alertController.addAction(okAction)



okAction.isEnabled = false


alertController.addTextField { (textField) in
    textField.placeholder = "Current Password"
    }


}
alertController.addTextField { (textField) in
    textField.placeholder = "New Password"
    }


}
alertController.addTextField { (textField) in
    textField.placeholder = "Confirm Password"
    textField.isSecureTextEntry = true
    NotificationCenter.default.addObserver(forName: NSNotification.Name.UITextFieldTextDidChange, object:textField,
                                           queue: OperationQueue.main) {
                                            (notification) -> Void in
                                            self.validateResetPassword(alertController: alertController,okAction: okAction)
    }



}

self.present(alertController, animated: true, completion: nil)

Can someone help me to fix this. Thanks in advance

Upvotes: 0

Views: 71

Answers (2)

Lal Krishna
Lal Krishna

Reputation: 16180

I recommend you to use Interface Builder(IB) to set UIBarButtonItems. You can also set Image via IB as well as Code.

  1. Set UIBarButtonItem on Interface Builder
  2. Use Code:

-

let editBtn:UIBarButtonItem = UIBarButtonItem(image: UIImage(named: IMAGE_EDIT_ICON), style: .plain, target: self, action:#selector(handleEditAction)) self.navigationItem.rightBarButtonItem = editBtn

Upvotes: 0

KKRocks
KKRocks

Reputation: 8322

Replace your bar button as below :

let barButton =    UIBarButtonItem(image: UIImage(named: "imageName"), style: .plain, target: self, action: #selector(self.YourSelectorTarget))

Upvotes: 1

Related Questions