我有一个嵌套在容器中的视图控制器内部的滚动视图。使用指定类ScrollingViewController
的视图控制器如下所示:
class ScrollingViewController: UIViewController {
@IBOutlet weak var scrollView: UIScrollView! //outlet for the Scroll View
override func viewDidLoad() {
super.viewDidLoad()
// 1) Create the two views used in the swipe container view
var storyboard = UIStoryboard(name: "App", bundle: nil)
var subOne: SubProfileOneViewController = storyboard.instantiateViewControllerWithIdentifier("subone") as! SubProfileOneViewController
var subTwo: SubProfileTwoViewController = storyboard.instantiateViewControllerWithIdentifier("subtwo") as! SubProfileTwoViewController
// 2) Add in each view to the container view hierarchy
// Add them in opposite order since the view hierarchy is a stack
self.addChildViewController(subTwo);
self.scrollView!.addSubview(subTwo.view);
subTwo.didMoveToParentViewController(self);
self.addChildViewController(subOne);
self.scrollView!.addSubview(subOne.view);
subOne.didMoveToParentViewController(self);
// 3) Set up the frames of the view controllers to align
// with each other inside the container view
var adminFrame :CGRect = subOne.view.frame;
adminFrame.origin.x = adminFrame.width;
subTwo.view.frame = adminFrame;
// 4) Finally set the size of the scroll view that contains the frames
var scrollWidth: CGFloat = 2 * self.view.frame.width
var scrollHeight: CGFloat = 262
self.scrollView!.contentSize = CGSizeMake(scrollWidth, scrollHeight);
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
基本上正在发生的是,使用该类
SubProfileOneViewController
和SubProfileTwoViewController
的两个视图控制器分别被实例化为subOne
和subTwo
。然后将它们作为子级添加到滚动视图中,以创建一个接口,用户可以向右滑动来访问另一个视图(几乎像 Snapchat)。 subOne
和subTwo
应该并排并且用户应该能够从一个滚动到另一个。
SubProfileOneViewController
和SubProfileTwoViewController
各自有一个视图(分别由绿色和红色表示),并且每个视图具有相同的约束条件,即:高度= 262,距离父视图的右边界= 0,距离父视图的左边界= 0,距离父视图的顶部= 0