0

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 customerscreen vendorscreen

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..

arun_K
  • 313
  • 1
  • 4
  • 12
  • Hi, @ashwin, how do you layout your view? Do you use autolayout? – Alexander Tkachenko Oct 30 '15 at 14:16
  • Relative question http://stackoverflow.com/questions/19561269/autolayout-with-hidden-uiviews – Alexander Tkachenko Oct 30 '15 at 14:18
  • 1
    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. – n00neimp0rtant Oct 30 '15 at 14:27
  • @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 – arun_K Oct 31 '15 at 06:04
  • @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 – arun_K Oct 31 '15 at 06:29

3 Answers3

1

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.

Vijay
  • 791
  • 1
  • 8
  • 23
  • He wants to animate it – Gal Marom Oct 30 '15 at 14:28
  • @GalMarom, Thanks. Edited – Vijay Oct 30 '15 at 14:31
  • @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] (http://s11.postimg.org/ajruwronn/storyboardhierarchy.png) and let me know how to proceed further – arun_K Oct 31 '15 at 06:23
  • 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 Oct 31 '15 at 07:11
  • @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..? – arun_K Oct 31 '15 at 07:42
  • @ashwin Yes. That will be work out when you hidden the vendorID field. – Vijay Nov 01 '15 at 16:46
  • @Vijay I am getting the following as output after using your code and i am achieving the same output after using GalMarom code as well here is the snippet (http://s30.postimg.org/6da6xij41/endoutput.png) could you please look into it – arun_K Nov 05 '15 at 13:56
  • @GalMarom I have implemented it as per your suggestion but i am getting the following result as output (http://s14.postimg.org/6vwt6mv7l/endoutput.png) please go through code snippet (http://s4.postimg.org/kx6j3pmhp/codeforoutput.png) and let me know where am i going wrong – arun_K Nov 05 '15 at 13:56
  • @ashwin. Not possible. Can you show me the code? Did you changed the frame of view1. – Vijay Nov 05 '15 at 14:56
  • @ashwin share some screenshots – Vijay Nov 06 '15 at 06:10
  • @Vijay screenshots of code would be fine or would u like to even have storyboard along with it – arun_K Nov 06 '15 at 06:12
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/94389/discussion-between-vijay-and-ashwin). – Vijay Nov 06 '15 at 06:14
  • @Vijay i have recently posted another question could you please help me out – arun_K Nov 19 '15 at 06:00
0

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)

Gal Marom
  • 8,499
  • 1
  • 17
  • 19
  • 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 (http://s11.postimg.org/ajruwronn/storyboardhierarchy.png) if possible a working example will be appreciable. – arun_K Oct 31 '15 at 06:26
  • 1
    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. – Gal Marom Oct 31 '15 at 10:11
  • I have implemented it as per your suggestion but i am getting the following result as output (http://s14.postimg.org/6vwt6mv7l/endoutput.png) please go through code snippet (http://s4.postimg.org/kx6j3pmhp/codeforoutput.png) and let me know where am i going wrong – arun_K Nov 04 '15 at 14:59
  • 1
    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) – Gal Marom Nov 06 '15 at 15:35
0

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:

Blockquote

@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
    }
}

Blockquote

Shy Attoun
  • 23
  • 5