在iphone和ipad上放置不同的视图

时间:2016-03-15 14:58:48

标签: ios iphone swift uiscrollview uiimageview

我已经构建了一个应用程序,允许用户使用覆盖的眼镜选择他们的照片。它正是在ipad和ipad mini上完全按照我想要的方式工作但在iphone上滚动视图的位置是错误的(见图片)。

func setupPages(){
    pageImages = [
        UIImage(named: "glasses_one")!,
        UIImage(named: "glasses_two")!,
        UIImage(named: "glasses_three")!,
        UIImage(named: "glasses_four")!,
        UIImage(named: "glasses_five")!,
        UIImage(named: "glasses_six")!
    ]

    let pageCount = pageImages.count

    for _ in 0..<pageCount {
        pageViews.append(nil)
    }
    print(pageViews.count)

    let pagesScrollViewSize = scrollView.frame.size
    scrollView.contentSize = CGSize(width:  scrollWidth * CGFloat(pageImages.count),
        height: pagesScrollViewSize.height)

    scrollView.backgroundColor = UIColor.blueColor()

    loadVisiblePages()
}

func loadVisiblePages() {
    let pageWidth = scrollView.frame.size.width

    page = Int(floor((scrollView.contentOffset.x * 2.0 + pageWidth) / (pageWidth * 2.0)))
    print(page)
    pageControl.currentPage = page

    for index in 0...5 {
        //print(" -- Index = \(index)")
        loadPage(index)
    }
}

func loadPage(page: Int) {
    if page < 0 || page >= pageImages.count {
        // If it's outside the range of what you have to display, then do nothing
        return
    }

    // 1
    if let pageView = pageViews[page] {
        print(pageView.description)
        // Do nothing. The view is already loaded.
    } else {
        // 2
        var frame = scrollView.bounds
        frame.origin.x = scrollWidth * CGFloat(page)
        frame.origin.y = 0.0
        frame.size.width = scrollWidth
        // 3
        let newPageView = UIImageView(image: pageImages[page])
        newPageView.contentMode = .ScaleAspectFit
        newPageView.frame = frame
        newPageView.clipsToBounds = false
        scrollView.addSubview(newPageView)


        // 4
        pageViews[page] = newPageView
    }
}

ipad公司

enter image description here

iPhone

enter image description here

1 个答案:

答案 0 :(得分:1)

我认为你的问题是你需要添加一个约束来垂直居中你的图像。或者你也可以通过为你的“眼镜框架”框架设置宽度和高度来解决这个问题。较小的屏幕周围的空间较小,但仍处于中间位置。