1

I have designed screen using storyboard constraints for iPhone XR. if i run that on iPhone7 then it's keyboard hides textfields.. so i have return code for textfield up while keyboard appears.. but here textfield going up for every iPhone sizes.. here i want to move textfield up while keyboard enter according to screen size. how?

here is my code:

 func animateViewMoving (up:Bool, moveValue :CGFloat){
    let movementDuration:TimeInterval = 0.3
    let movement:CGFloat = ( up ? -moveValue : moveValue)
    UIView.beginAnimations( "animateView", context: nil)
    UIView.setAnimationBeginsFromCurrentState(true)
    UIView.setAnimationDuration(movementDuration )
    self.view.frame = self.view.frame.offsetBy(dx: 0,  dy: movement)
    UIView.commitAnimations()
}

func textFieldDidBeginEditing(_ textField: UITextField) {        
    if  textField == self.self.conformPasswordTextField {
        animateViewMoving(up: true, moveValue: 60)
    }
}  

func textFieldDidEndEditing(_ textField: UITextField) {

    if  textField == self.self.conformPasswordTextField {
        animateViewMoving(up: false, moveValue: 60)
    }
 }

How to move up textfield according to iPhone sizes.

Please help me in the code.

Reed
  • 944
  • 7
  • 15
Swift
  • 1,074
  • 12
  • 46

2 Answers2

0

The best way to design and develop applications is to ALWAYS develop an application on the SMALLEST screen size. Scaling up applications is always easier then it is to scale an app down.

Based on your question, what you are asking to do is (other ways of doing this) creating variables that incorporate all the screen sizes width and height, and setting each object width height and so on according to the screen size.

This is not best practice. The best way to do this is to use 'vary for traits' in XCode Story board. What is 'Vary for Traits' in Xcode 8? Referencing this question above is a good place to start.

However my recommendation is to redesign your application to fit on the SMALLEST ios screen size you are willing to support.

Like I said it is always much easier to scale up then to scale down and up

Julian Silvestri
  • 1,970
  • 1
  • 15
  • 33
0

If you don't want to use auto layout and wants textfield to go up when keyboard appears then use UIScrollView.

and use following code. make sure you change it according to your text fields call this function registerforkeyboardnotifiations from your viewdidload

  lazy var securityScrollView = UIScrollView()
 var activeTextField = UITextField()
  var testTextField = UITextField()

 //call setup screen from viewdidload
func setUpScreen {

  view.addSubview(securityScrollView)

  // adding textfield to scroll view
  securityScrollView.addSubview(testTextField)

  // make sure to setup constraints. for textfield and scrollview

}


  /// here I am setting my activekeyboard
    public func textFieldDidBeginEditing(_ textField: UITextField) {

    activeTextField = textField
}

 func registerForKeyboardNotifications() {

    NotificationCenter.default.addObserver(self, selector: #selector(onKeyboardAppear(_:)), name: UIResponder.keyboardDidShowNotification, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(onKeyboardDisappear(_:)), name: UIResponder.keyboardDidHideNotification, object: nil)
}

 @objc func onKeyboardAppear(_ notification: NSNotification) {

    let info = notification.userInfo!
    let rect: CGRect = info[UIResponder.keyboardFrameBeginUserInfoKey] as! CGRect
    let kbSize = rect.size
    let insets = UIEdgeInsets(top: 0, left: 0, bottom: kbSize.height+120, right: 0) // bottom constants change according to your needs
    self.securityScrollView.contentInset = insets
    self.securityScrollView.scrollIndicatorInsets = insets
    var visibleRect: CGRect = self.securityScrollView.convert(self.securityScrollView.bounds, to: self.view)
    visibleRect.size.height -= rect.size.height;
    let inputRect: CGRect = self.activeTextField.convert(self.activeTextField.bounds, to: self.securityScrollView)
    if (visibleRect.contains(inputRect)) {
        self.securityScrollView.scrollRectToVisible(inputRect, animated: true)
    }
}

@objc func onKeyboardDisappear(_ notification: NSNotification) {

    self.securityScrollView.contentInset = UIEdgeInsets.zero
    self.securityScrollView.scrollIndicatorInsets = UIEdgeInsets.zero
}

make sure you are removing observer and best place to do it is in deist if you are using view controller

 deinit {
    NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardDidShowNotification, object: nil)
    NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardDidHideNotification, object: nil)
}

If you want to setup your scrollview refer this

 public override func viewWillLayoutSubviews() {

    super.viewWillLayoutSubviews()
    securityScrollView.contentSize = CGSize(width: self.view.frame.width, height: self.view.frame.height + kPaddig)
    securityScrollView.isScrollEnabled = true
    securityScrollView.showsVerticalScrollIndicator = false
}

change active textfield for your text field

 var activeTextField = UITextField()

Make changes according to your scrollview and textbook. Enjoy!

Reed
  • 944
  • 7
  • 15
  • can you please show one total example about scrollview for textfield to go up when keyboard appears.. because i never worked before with scrollview.. i have gone through some youtube tutorial still i diid not understand – Swift Oct 21 '19 at 17:21
  • sure updating answer for generic so that it will work for all textboxes – Reed Oct 21 '19 at 17:25
  • thank you! `Use of unresolved identifier 'kPaddig'` error – Swift Oct 21 '19 at 17:47
  • padding is any float value. or you can declare at top let kPaddig: CGFloat = 20 – Reed Oct 21 '19 at 18:03