0

I am making an app that needs to create a Registration Form to take user inputs. For inputs I have 4 fields. I adjusted 4 fields in UIStackView and made stakeview vertically centered as it was a design of app. Everything is going good. but in small devices Keyboard was over lapping some fields.

To solve my problem I did R&D and used IQKeyboardManager. It helped me a lot. but I have seen that in fields all you can go to other fields you have to use toolbar provided by IQKeyboardManager. But now a days in advance app you can see you can scroll the view up and down.

What I have done: So To solve my problem I created a UIScrollView around my stackview that contains my fields but it never helped me. Because it is creating design issues also. Stackview never comes in vertically centered of ScrollView. also scrollview is enabled all the time regardless of keyboard is appeared or not. look at following picture that dipicts me UI

enter image description here

What I want: All I want is that untill and unless Keyboard is appeared scrollview should not be scrollable. Once the keyboard is appeared then the scrollview must be scrollable. Also I want to check if there is need to make scrollview scrollable or not. Because in larger devices like iPhone 8plus I think there is no need to show scrollview as keyboard is always below the last UITextfield. Its just overlapping in smaller phone like iPhone 5.

Please help me. It is my idea that apps are using scrollview. They may be using something other that act lik scrollview aroung views when Keyboard is open and does not show any scrollview when there is no keyboard open.

Rishil Patel
  • 1,977
  • 3
  • 14
  • 30
A.s.ALI
  • 1,992
  • 3
  • 22
  • 54

2 Answers2

0

Use keyboardWillShowNotification and keyboardWillHideNotification notification to change the scrollView height. On keyboardWillShowNotification you decrease the scrollView height by the height of the keyboard. And in keyboardWillHideNotification you just reset the height as it was before. Any doubt please comment.

vivekDas
  • 1,248
  • 8
  • 12
  • please show me code. And also how to center align the views inside the UiScrollview. I have UIStackView and it is not getting center aligned vertically inside the UiScrollview – A.s.ALI Nov 28 '18 at 05:25
0
  1. Create a content view inside scroll view.
  2. Set content view constraint (top, bottom, leading and trailing) as (0,0,0,0) and equal width and equal height with scroll view.

To set constraint on scroll view, you can refer

https://medium.com/@pradeep_chauhan/how-to-configure-a-uiscrollview-with-auto-layout-in-interface-builder-218dcb4022d7

After that, implement keyboard observer in your view controller.

override func viewDidLoad()
    {
    super.viewDidLoad()
    NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardDidShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardDidHide, object: nil)
}

 func keyboardWillShow(notification: NSNotification) {
      // set scroll view content size height as per your need (subtract keyboard height from original)
}

func keyboardWillHide(notification: NSNotification) {
    // reset scroll view content size height to original
}
Viren Malhan
  • 115
  • 1
  • 5
  • and how to vertically center align my fields in the scrollview – A.s.ALI Nov 28 '18 at 05:33
  • put your stack view inside content view of scroll view with vertically centred to parent. that can solve your alignment problem. and set scroll view constraint (top, bottom, leading and trailing) as (0,0,0,0). – Viren Malhan Nov 28 '18 at 05:37
  • I have done this too. but problem is something I cant understand. By doing this I am able to see my vertically centered fields. but when I do ran on real device my whole Stackview shifts to bottom. Looks like on real device it is unable to find the center of vertical in UiScrollview – A.s.ALI Nov 28 '18 at 05:40