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