【问题标题】:How can you make a .xib file fill the whole screen width (Swift)如何让 .xib 文件填满整个屏幕宽度(Swift)
【发布时间】: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,然后调用 loadScrollViewViewsself.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


【解决方案1】:

尝试为你的滚动视图添加一些约束

【讨论】:

  • 我在情节提要中的 scrollView 中添加了一些约束。而且我认为这不是scrollView的问题,而不是没有正确调整的xib文件。
猜你喜欢
  • 1970-01-01
  • 2015-05-15
  • 2021-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多