hiding and adjusting of textfields based on view iOS

how to hide and show view in ios
how to use auto-layout to move other views when a view is hidden?
swift get input from text field
show/hide password text view—in swift!
autolayout collapse when hidden
uitextfield editing changed programmatically

I am naive in iOS. I am developing a registration page which consists of hiding textfields based on views. In order to achieve this i implemented UISegmentedControl for views and used the following code for hiding the textfield based on segments

- (IBAction)regSegmentButton:(id)sender {

if (_registrationSegment.selectedSegmentIndex ==1) {
    self.vendorID.hidden = NO;
else {
    self.vendorID.hidden = YES;


Even though i achieved what i intended but the view is absurd. The view is represented below in images

My question is how to make the view look normal even after hiding the field same as the way it is represented in "vendorscreen.png"(second image). Do i need to apply any animation in order to achieve it? if yes please let me know how to do it

Please do the needful..

You can do it with / without animation. Here is the step for without animation. You have to add your email-id, password, confirm password and register button to normal UIView as for eg: view1 then when you change the switch you have to adjust the 'y' position of view1. Like below

- (IBAction)regSegmentButton:(id)sender {

    if (_registrationSegment.selectedSegmentIndex ==1) {
        self.vendorID.hidden = NO;
    else {
        self.vendorID.hidden = YES;

    int y = (_registrationSegment.selectedSegmentIndex == 1) ? 200 : 150
    CGRect rect = self.view1.frame;
    rect.origin.y = y;
    self.view1.frame = rect;

I've just hard coded the value for understanding, You need to adjust the y value based on your view.

How to use auto-layout to move other views when a view is hidden , How do you use auto layout to move other views when a view is hidden? Because the keyboard can hide portions of your interface, you should use the size information to reposition your content on the screen. For content embedded in a scroll view, you can scroll the text field into view, as illustrated in Figure 2. In other cases, you can resize your main content view so that it is not covered by the keyboard. Figure 2

One solution is: You can have a UIView that contains all the bottom UITextField. All you have to do is animate the new view to the hidden view position.

    [UIView animateWithDuration:0 animations:^(){
    textFieldContainers.frame = CGRectMake(textFieldContainers.frame.origin.x, textFieldContainers.frame.origin.y - vendorIDTextField.frame.size.height, textFieldContainers.frame.size.width, textFieldContainers.frame.size.height);

The other one is to work with constrains (encapsulate the 'static' view in one view container will be helpful in this solution as well)

ios How to disable UITextField editing but still accept touch?, How do I disable UITextField editing but still accept touch? Managing the Keyboard. When users touch a text field, a text view, or a field in a web view, the system displays a keyboard. You can configure the type of keyboard that is displayed along with several attributes of the keyboard. You also have to manage the keyboard when the editing session begins and ends.

enter image description herereferring to gal marom answer you can also put the remained TextFields in UIView [on storyboard]and use constraint from it to your Segmented view, then you should click on the top constraint itself [constrained to the segmented view] and create an outlet of it in your code ,then in your segmented action's condition do the following:


@IBOutlet weak var fullnameTF: UITextField!//* by itself

@IBOutlet weak var userEmail: UITextField! //* in the same UIView

@IBOutlet weak var userPassword: UITextField! //* in the same UIView

@IBOutlet weak var viewStackTopCont: NSLayoutConstraint!

@IBAction func segAction(_ sender: Any) {
    if seg.selectedSegmentIndex == 0 {

        fullnameTF?.isHidden = true
        viewStackTopCont.constant = 20 // after the TextField is hidden

    }else if seg.selectedSegmentIndex == 1{
        fullnameTF?.isHidden = false
        viewStackTopCont.constant = 60 //your current constraint margin


hide/show text fields with auto layouts and programmatically , how to hide text field that fills the existing text field in iOS, with auto layout. hide/​show text Duration: 10:53 Posted: Nov 16, 2016 Handling the Keyboard Animations in SwiftUI with ObservedObject Published by iOS App Templates on March 23, 2020 While working on the SwiftUI Chat tutorial, we ran into the issue of handling the keyboard animations by adjusting the layout of all the views visible on the screen, depending on whether the keyboard is visible or not.

UITextField, Text fields provide a built-in overlay view to clear the current text. Because the system manages the showing and hiding of the keyboard in Figure 2 Adjusting the position of a text field when the keyboard appears. For more information about managing keyboard interactions, see Text Programming Guide for iOS. Use secure text fields when asking for sensitive data, like a password. A secure text field obscures the entered text by replacing it with bullets. Let the user adjust text attributes if it makes sense. If your text field contains styled text, it may add value if the user can adjust the font, size, and color of the text.

Managing Text Fields and Text Views, Describes the technologies and techniques for displaying and managing text and web content in iOS. For this code example, I’ll wrap two UITextFields inside a scrollview. When the keyboard is shown, the scrollview size is adjust based on the size of the keyboard, allowing the textfield selected to be visible on the display. Let’s look at the view controller that will contain the textfields and the scrollview:

Adjusting The Layout Based on Keyboard With , Let's see how we handle keyboard animations and transitions in SwiftUI. is that we will notify the view containing the text fields that the keyboard needs a in SwiftUI: hiding and showing it, adjusting the layout based on its  Managing Text Fields and Text Views Text fields and text views have two main functions: to display text and to enable the entry and editing of text. Several programming tasks are associated with these simple purposes, including configuring the text object, accessing the current text, validating what the user enters, and displaying overlay views

  • Hi, @ashwin, how do you layout your view? Do you use autolayout?
  • Relative question stackoverflow.com/questions/19561269/…
  • You might find it easier to design your view controller as a UITableViewController instead. You could have 3 prototype cells ("segmented control cell", "text field cell", "register button cell"), and create and format it at runtime by implementing the tableView:cellForRowAtIndexPath: method in UITableViewDataSource. And since UITableView subclasses UIScrollView, you won't have to worry about managing scroll content size/inset stuff manually.
  • @AlexanderTkachenko thank you for your response. yes i used auto layout. As said earlier i am naive in this kind of implementation even to iOS if you can give me a basic example code it would be helpful
  • @n00neimp0rtant Thank you for your response i might be able to implement you response but as i am new to this development, for me adjusting all those according to my requirement might be bit high. still thank you for your kind response
  • He wants to animate it
  • @GalMarom, Thanks. Edited
  • @vijay thank you for your response animation or without animation is fine with me but i am kind of naive in this kind of approach which you are explaining please go through my storyboard hierarchy link [storyboard hierarchy] (s11.postimg.org/ajruwronn/storyboardhierarchy.png) and let me know how to proceed further
  • I understand that your constraints are set up with ventured filed. It will make problem when you hidden this field. Once you've moved all four element to view1 then you need to set the constraints to view1 at your regSegmentButton method programmatically.
  • @Vijay are u asking me to create another view inside my background and then add the textfields and buttons. later on u want me to add ur code is that so..?
  • thank you for your response could you please help me out how to implement this kind of approach by going through my story board hierarchy (s11.postimg.org/ajruwronn/storyboardhierarchy.png) if possible a working example will be appreciable.
  • Create a UIView and add all the field between email and registration inside it (just drag them inside the view). Create an outlet at your UIViewController class for this view. When you want to animate this view just call the code I've posted.
  • I have implemented it as per your suggestion but i am getting the following result as output (s14.postimg.org/6vwt6mv7l/endoutput.png) please go through code snippet (s4.postimg.org/kx6j3pmhp/codeforoutput.png) and let me know where am i going wrong
  • You don't change the y. You animate the frame to be the same one as before. In addition the duration is 0 so it will be immediate and won't really show any animation anyway. Try to change the y position (the second element in the CGRectMake and follow its behaviour)