这里我用xib来举例
1.先往xib中拖一个scrollView
设置当前scrollView上左下右与父视图约束为0;宽度任意设置一个固定值
2.再拖一个view到scrollview上,设置view与scrollView的上下左右约束为0,设置此view的宽度等于scrollView ,这时候系统会报约束错误
我们将scrollView上的这个子view的intrinsic size 修改成placeholder,并设置任意宽高,因为宽高是随着子控件的宽高自动布局的
tips:考虑到部分视图比较长,可以将当前控制器的view的size设置为freeform,这样可以更直观的查看显示效果
跳转是在控制器之间发生的,应该是由一个控制器跳转到另一个控制器,跳转后就可以把当前的控制器默认的View添加到窗口上,而不是由UIView跳转到控制器,不要犯这样的错;控制器跳转办法:
1、用UINavigationController 的pushViewController
2、用self的presentViewController
3、再就是用storyBoard的segue进行跳转
基本就这三个,有问题再问。
使用默认的ViewController作为盛放childController 的容器。创建FirstViewController作为ChildViewController。在这里我只是简单的实验了一下两个控制器之间的跳转,其中并没有加入数据。因为两个控制器是分离开的。所以很大程度上降低了耦合度。
1 在viewcontroller中
@property(nonatomic, strong) UIViewController *currentVC记录当前的控制器 将viewcontroller 作为当前的控制器 _currentVC = self
2 创建点击按钮,点击d出childViewController
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]
3 将firstViewController声明为全局变量。以便后续 *** 作。
4 点击viewcontroller中的按钮 响应事件如下
- (void)btnClick:(UIButton *)btn {
[self addChildViewController:firstVC]
[self.view addSubview:firstVC.view]
[UIView animateWithDuration:2.0 delay:0.5 usingSpringWithDamping:0.5 initialSpringVelocity:0.5 options:UIViewAnimationOptionCurveEaseInOut animations:^{
firstVC.baseView.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2)
} completion:^(BOOL finished) {
NSLog(@"完成")
}]
}
********重要的点在点击按钮时 ,按钮响应方法中 [self addChildViewController:_firstVC]
和[self.view addSubview:_firstVC.view]一个是讲firstVC作为当前控制器的子视图控制器。另一个addSubview方法是讲firstVC的视图添加到当前视图,作为子视图显示。在firstVC视图出现时,我添加了动画效果。
5 在firstViewControl中 需要有回调方法。在这里我用的block 在firstViewController中我添加了一个点击按钮。点击按钮会将Block传到viewController中
- (void)btnClick:(UIButton *)btn {
self.firstBlock()
}
6 在viewcontroller中 接受childViewcontroller传过来的block
firstVC = [[FirstViewController alloc] init]
__weak FirstViewController *weakSelf = firstVC
firstVC.firstBlock = ^(){
[weakSelf removeFromParentViewController]
[UIView animateWithDuration:2.0 delay:0.5 usingSpringWithDamping:0.5 initialSpringVelocity:0.5 options:UIViewAnimationOptionCurveEaseInOut animations:^{
weakSelf.baseView.frame = CGRectMake(0, -weakSelf.baseView.frame.size.height, weakSelf.baseView.frame.size.width, weakSelf.baseView.frame.size.height)
} completion:^(BOOL finished) {
[weakSelf.view removeFromSuperview]
_currentVC = weakSelf
NSLog(@"完成")
}]
}
在这里由于用到动画,所以涉及到firstViewController中的frame。在这里就会牵扯到block的循环引用问题了。 __weak FirstViewController *weakSelf = firstVC用__weak修饰自身。weak是弱引用,不会持有对象。所以在block里面使用self时就不会再造成相互持有。retain cycle了。[weakSelf removeFromParentViewController]firstVC 从当前视图控制器中移除。在动画完成时 将当前的currentVC = self
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)