How to Push one UIview to another UIview in ios us

2019-09-17 03:32发布

问题:

Hi I am very new in iOS and in my project I have added two UIViews programmatically on my main UIViewController.

Here my main requirement is when I click "Next" button which is in my firstView, I want to push the FirstView to SecondView and when I click "Back" button which is in the SecondView, I want to pushBack secondView to firstView.

As in how to push one UIViewController to another UIViewController?

But same requirement I want to do Using UIView programmatically.

Please help me.

How can we do this?

my code:-

#import "ViewController.h"

@interface ViewController ()
{
    UIView * FisrtView;
    UIView * SecondView;

    UIButton * Next;
    UIButton * Back;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    FisrtView = [[UIView alloc] init];
    FisrtView.backgroundColor=[UIColor lightGrayColor];
    FisrtView.translatesAutoresizingMaskIntoConstraints = NO;

    [self.view addSubview:FisrtView];

    SecondView = [[UIView alloc] init];
    SecondView.backgroundColor=[UIColor orangeColor];
    SecondView.translatesAutoresizingMaskIntoConstraints = NO;

    [self.view addSubview:SecondView];

    Next = [[UIButton alloc] init];
    Next.backgroundColor=[UIColor orangeColor];
    Next.translatesAutoresizingMaskIntoConstraints = NO;
    [Next setTitle:@"Go Next" forState:UIControlStateNormal];
    [Next addTarget:self action:@selector(Next:) forControlEvents:UIControlEventTouchUpInside];

    [FisrtView addSubview:Next];

    Back = [[UIButton alloc] init];
    Back.backgroundColor=[UIColor lightGrayColor];
    Back.translatesAutoresizingMaskIntoConstraints = NO;
    [Back setTitle:@"Go Back" forState:UIControlStateNormal];
    [Back addTarget:self action:@selector(Back:) forControlEvents:UIControlEventTouchUpInside];

    [SecondView addSubview:Back];


    //Applting Autolayouts for All Fields

    NSDictionary * HeaderDictionary = NSDictionaryOfVariableBindings(FisrtView,SecondView,Next,Back);

    //Appliying Autolayouts for FirstView

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:|-0-[FisrtView]-0-|"]
                                                                        options:0
                                                                        metrics:nil
                                                                          views:HeaderDictionary]];

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:|-0-[FisrtView]-0-|"]
                                                                      options:0
                                                                      metrics:nil
                                                                        views:HeaderDictionary]];

    //Appying Autolayoust for NextButton

    [FisrtView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:|-10-[Next]-10-|"]
                                                                      options:0
                                                                      metrics:nil
                                                                        views:HeaderDictionary]];

    [FisrtView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:|-100-[Next(30)]"]
                                                                      options:0
                                                                      metrics:nil
                                                                        views:HeaderDictionary]];

     //Appliying Autolayouts for secondView

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:|-0-[SecondView]-0-|"]
                                                                      options:0
                                                                      metrics:nil
                                                                        views:HeaderDictionary]];

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:|-0-[SecondView]-0-|"]
                                                                      options:0
                                                                      metrics:nil
                                                                        views:HeaderDictionary]];

    //Appying Autolayoust for BackButton

    [SecondView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:|-10-[Back]-10-|"]
                                                                      options:0
                                                                      metrics:nil
                                                                        views:HeaderDictionary]];

    [SecondView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:|-100-[Back(30)]"]
                                                                      options:0
                                                                      metrics:nil
                                                                        views:HeaderDictionary]];


    SecondView.hidden = YES;
    FisrtView.hidden = NO;

}

-(void)Next:(id)sender{

    SecondView.frame=CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);  //your view start position

[UIView animateWithDuration:0.15f
                      delay:0.0f
                    options:UIViewAnimationOptionBeginFromCurrentState
                 animations:^{
                     [SecondView setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];   // final visible position
                 }
                 completion:nil];
SecondView.hidden = NO;
}

-(void)Back:(id)sender{

    SecondView.hidden = YES;
    FisrtView.hidden = NO;
}

@end

回答1:

initial mistake, you were create the button is wrong and does not set the frame to visible area

not like

Next = [[UIButton alloc] init];

do like

Next = [UIButton buttonWithType: UIButtonTypeRoundedRect];
Next.frame = CGRectMake(210, 285, 100, 18)];
Next.translatesAutoresizingMaskIntoConstraints = NO;
[Next setTitle:@"Go Next" forState:UIControlStateNormal];
[Next addTarget:self action:@selector(Next:) forControlEvents:UIControlEventTouchUpInside];
[FisrtView addSubview:Next];

and forget to add frame for visible area in view

FisrtView = [[UIView alloc] init];
FisrtView.frame = CGRectMake(210, 285, 100, 18)];

SecondView = [[UIView alloc] init];
SecondView.frame = CGRectMake(210, 285, 100, 18)];

for sample animation

-(void)Next:(id)sender{

SecondView.frame=CGRectMake(248, -420, 500, 414);  //your view start position

[UIView animateWithDuration:0.5f
                      delay:0.0f
                    options:UIViewAnimationOptionBeginFromCurrentState
                 animations:^{
                     [SecondView setFrame:CGRectMake(248, 30, 500, 414)];   // final visible position
                 }
                 completion:nil]; 
}

for sample animation see this link



回答2:

Please refer following link. hope this will help you.

http://googleweblight.com/?lite_url=https://stackoverflow.com/questions/2215672/how-to-change-the-push-and-pop-animations-in-a-navigation-based-app&ei=Qc-ODruI&lc=en-IN&s=1&m=775&ts=1447514312&sig=APONPFlwklDRNHSybRHp3xVcrDKmQUeRjg