我设置了一个 UIScrollView
,其中包含两个 ViewController 的
。这是我的 viewDidLoad
的样子:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
scrollView.delegate = self
self.automaticallyAdjustsScrollViewInsets = false
let allViewController = self.storyboard!.instantiateViewControllerWithIdentifier("All") as! AllViewController;
let favoritesViewController = self.storyboard!.instantiateViewControllerWithIdentifier("Favorites") as! FavoritesViewController;
scrollView!.contentSize = CGSizeMake(2*CGRectGetWidth(allViewController.view.frame), CGRectGetHeight(scrollView!.frame));
let viewControllers = [allViewController, favoritesViewController]
var idx:Int = 0;
for viewController in viewControllers {
addChildViewController(viewController);
let originX:CGFloat = CGFloat(idx) * CGRectGetWidth(scrollView!.frame);
viewController.view.frame = CGRectMake(originX, 0, viewController.view.frame.size.width, viewController.view.frame.size.height);
scrollView!.addSubview(viewController.view)
viewController.didMoveToParentViewController(self)
idx++;
}
在 iPhone 6 模拟器上一切正常,但是当我在 iPhone 5 模拟器上运行它时,滚动时 Controller 之间有间隙:
[ ] scrollView 的前导、尾随、顶部和底部的自动布局设置为 0。
最佳答案
如果您想在 viewDidLoad:
中设置起点,并且您肯定知道您的 ScrollView 将具有屏幕的宽度,那么在您的计算逻辑中而不是 CGRectGetWidth(scrollView!.框架)
使用
var bounds = UIScreen.mainScreen().bounds
var width = bounds.size.width
为了在您的 subview 进行布局之前获得适当的宽度。
更新:
正如这里有人提到的,我取消选中 ScrollView 的 Autoresize Subviews
属性,子类化 UITableViewCell
并将我的提交推送到 github。通过我上面提供的链接进行检查。这是按钮右对齐的结果:
结果:
引用: 我的 SO 回答 here 中描述了主要概念
关于ios - 为什么水平 ScrollView 内容之间存在间隙?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31657743/