【发布时间】:2018-04-14 08:20:36
【问题描述】:
我想要一个包含其他视图的 UIScrollView。一个是 UITableView,另一个是 MKMapView。为此,我创建了两个 xib 文件。当我想将这两个添加到我的滚动视图时,我会这样做:
func setupScrollView() {
scrollView.isPagingEnabled = true
scrollView.contentSize = CGSize(width: self.view.bounds.width * 2, height: self.view.bounds.height)
scrollView.showsHorizontalScrollIndicator = false
scrollView.delegate = self
}
func loadScrollViewViews() {
if let tableView = Bundle.main.loadNibNamed("ViewTableShow", owner: self, options: nil)?.first as? ViewTableShow {
scrollView.addSubview(tableView)
tableView.frame.size.width = self.view.bounds.size.width
tableView.frame.origin.x = 0
}
if let mapView = Bundle.main.loadNibNamed("ViewMapShow", owner: self, options: nil)?.first as? ViewMapShow {
scrollView.addSubview(mapView)
mapView.frame.size.width = self.view.bounds.size.width
mapView.frame.origin.x = self.view.bounds.width
}
}
首先我在 ViewController 类的 viewDidLoad 函数中调用 setupScrollView,然后调用 loadScrollViewViews。 self.view.bounds.size.width 给了我正确的屏幕宽度大小,但似乎没有正确设置两个视图的宽度。
Here 看起来是正确的。我想覆盖整个屏幕宽度。有人知道怎么做吗?
【问题讨论】:
-
看起来 scrollView.frame.size.width 是错误的。你在哪里设置scrollView的frame?
-
我不这么认为,否则每个视图的起始位置也会出错,但这不是问题。
-
你在哪里以及如何设置scrollView的frame?你也可以检查框架吗?
-
非常感谢!我在这里坐了好几个小时才发现我没有用
scrollView.frame.size.width = self.view.bounds.width设置框架宽度。现在可以了!
标签: swift uiview uiscrollview constraints xib