【问题标题】:Paging UIScrollView seems to place content off centre分页 UIScrollView 似乎使内容偏离中心
【发布时间】:2016-07-11 19:06:32
【问题描述】:

我有一个 UIScrollView,我希望它具有分页功能(想想初始启动屏幕)。我希望将内容(UILabel 和 UIImageView)集中放置在滚动视图的每个分页视图中。我的问题是它总是稍微偏离中心 ()。

完整代码如下:

  var splashScreenObjects = [SplashScreenObject]()

  var imageViewArray = [UIImageView]()
  var subtitleViewArray = [UILabel]()

  @IBOutlet var scrollView: UIScrollView!
  @IBOutlet var pageControl: UIPageControl!

  override func viewDidLoad() {
    super.viewDidLoad()
    createSplashScreenObjects()
    configurePageControl()
    configureScrollView()
  }

  func createSplashScreenObjects() {
    let firstScreen: SplashScreenObject = SplashScreenObject(subtitle: "Medication reminders on your phone. Never miss your next dose", image: UIImage(named: "splashScreen1")!)
    let secondScreen: SplashScreenObject = SplashScreenObject(subtitle: "Track how good you have been with your medication", image: UIImage(named: "splashScreen2")!)
    let thirdScreen: SplashScreenObject = SplashScreenObject(subtitle: "The better you are with your medication, the more points you'll earn!", image: UIImage(named: "splashScreen3")!)
    splashScreenObjects.append(firstScreen)
    splashScreenObjects.append(secondScreen)
    splashScreenObjects.append(thirdScreen)

  }

  func configureScrollView() {
    self.scrollView.layoutIfNeeded()

    self.scrollView.showsHorizontalScrollIndicator = false
    self.scrollView.showsVerticalScrollIndicator = false

    self.scrollView.pagingEnabled = true
    self.scrollView.delegate = self

    let width = view.frame.size.width

    for index in 0..<splashScreenObjects.count {
      let subtitle = UILabel(frame: CGRectMake((width * CGFloat(index)) + 25, self.scrollView.frame.size.height-75, width-50, 75))
      subtitle.text = splashScreenObjects[index].subtitle
      subtitle.textAlignment = NSTextAlignment.Center
      subtitle.textColor = UIColor.whiteColor()
      subtitle.font = UIFont(name:"Ubuntu", size: 16)
      subtitle.numberOfLines = 2
      subtitle.backgroundColor = UIColor.clearColor()
      self.scrollView.addSubview(subtitle)
      self.subtitleViewArray.append(subtitle)
      subtitle.alpha = 0

      let mainImage = UIImageView(frame: CGRectMake((width * CGFloat(index)), 50, width, self.scrollView.frame.size.height-150))
      mainImage.image = splashScreenObjects[index].image
      mainImage.contentMode = UIViewContentMode.ScaleAspectFit
      self.scrollView.addSubview(mainImage)
      self.imageViewArray.append(mainImage)
      mainImage.alpha = 0
    }

    self.scrollView.contentSize = CGSizeMake(width * CGFloat(splashScreenObjects.count), self.scrollView.frame.size.height-50)
    animateViews(Int(0))

  }

  func configurePageControl() {
    self.pageControl.numberOfPages = splashScreenObjects.count
    self.pageControl.currentPage = 0
    self.view.addSubview(pageControl)
    pageControl.addTarget(self, action: #selector(SplashViewController.changePage(_:)), forControlEvents: UIControlEvents.ValueChanged)
  }

  func changePage(sender: AnyObject) -> () {
    let x = CGFloat(pageControl.currentPage) * self.view.frame.size.width
    scrollView.setContentOffset(CGPointMake(x, 0), animated: true)
  }

  func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
    let pageNumber = round(scrollView.contentOffset.x / self.view.frame.size.width)
    pageControl.currentPage = Int(pageNumber)
    animateViews(Int(pageNumber))
  }

  func animateViews(pageNumber: Int) {
    UIView.animateWithDuration(0.5, animations: {
      self.imageViewArray[pageNumber].alpha = 1.0
      self.subtitleViewArray[pageNumber].alpha = 1.0
    })
  }

这是我对 UIScrollView 的自动布局约束:

【问题讨论】:

  • 在这种情况下,更好的方法是使用 UICollectionView。

标签: ios iphone swift uiscrollview


【解决方案1】:

您的前导和尾随空格都是-20,这意味着滚动视图比其父视图宽 40 点。将这些更改为 0。

【讨论】:

  • 这不是真的,-20 是因为约束是相对于 suoerview 的边距(即 20)。 -20 与将约束设置为 0 到超级视图的边界相同。
  • 这也是我的理解,但我不是专家
【解决方案2】:

你应该替换

self.scrollView.layoutIfNeeded()

self.view.layoutIfNeeded()

因为 layoutIfNeeded 布局调用者子视图,而不是它本身。所以,scrollView,当你在其上添加 subtitle 和 mainImage 时,有错误的框架。

【讨论】:

  • let width = view.frame.size.width插入print(width)print(self.scrollView.frame)之后试试,结果如何?
猜你喜欢
  • 1970-01-01
  • 2014-07-03
  • 1970-01-01
  • 2011-06-07
  • 1970-01-01
  • 1970-01-01
  • 2016-12-03
  • 2010-11-21
  • 1970-01-01
相关资源
最近更新 更多