您好我是iOS新手,在我的项目中,我已在主UIViews
上以编程方式添加了两个UIViewController
。
这里我的主要要求是当我点击"下一步"我firstView
中的按钮,我想将FirstView
推送到SecondView
和
当我点击"返回" SecondView
中的按钮,我想将后座secondView
推送到firstView
。
如何将一个UIViewController
推送到另一个UIViewController
?
但我想要以编程方式使用UIView
做同样的要求。
请帮帮我。
我们怎么做?
#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
答案 0 :(得分:1)
最初的错误,你创建的按钮是错误的,并没有将框架设置为可见区域
不喜欢
Next = [[UIButton alloc] init];
喜欢
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];
并忘记在视图中添加可见区域的框架
FisrtView = [[UIView alloc] init];
FisrtView.frame = CGRectMake(210, 285, 100, 18)];
SecondView = [[UIView alloc] init];
SecondView.frame = CGRectMake(210, 285, 100, 18)];
用于示例动画
-(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];
}
对于示例动画,请参阅此link
答案 1 :(得分:0)