如何在 UIPageViewController 中全屏呈现图像?

问题描述 投票:0回答:1

我正在尝试在 UIPageViewController 中呈现完整图像。每个图像都在单独的视图控制器中。我将 contentMode 设置为 scaleAspectFill。问题是,当我向后滚动时,两个图像像这样合并 https://i.imgur.com/CSgJLWq.png。我也尝试给每张图片设置宽度,但没有任何变化

private lazy var imageView: UIImageView = {
    let imageView = UIImageView()
    imageView.image = UIImage(named: "onboarding-concert")
    imageView.contentMode = .scaleAspectFill
    //imageView.frame.size.width = 400
    return imageView
}()

private func setUI() {
    view.addSubview(imageView)
    imageView.snp.makeConstraints { make in
        make.left.equalToSuperview()
        make.right.equalToSuperview()
        make.top.equalToSuperview()
        make.bottom.equalToSuperview()
    }
    
    view.addSubview(label)
    label.snp.makeConstraints { make in
        make.top.equalToSuperview().inset(90)
        make.left.equalToSuperview().inset(40)
    }
}

UIPageViewController 代码

class OnboardingViewController: UIPageViewController {

var pages = [UIViewController]()
let pageControl = UIPageControl()
let initialPage = 0
var pageControlBottomAnchor: NSLayoutConstraint?

override func viewDidLoad() {
    setup()
    style()
    layout()
}

func setup() {
    
    dataSource = self
    delegate = self
    
    pageControl.addTarget(self, action: #selector(pageControlTapped(_:)), for: .valueChanged)

    let page1 = FirstPageViewController()
    let page2 = SecondPageViewController()
    let page3 = ThirdPageViewController()
    
    pages.append(page1)
    pages.append(page2)
    pages.append(page3)
    
    setViewControllers([pages[initialPage]], direction: .forward, animated: true, completion: nil)
}

func style() {
    pageControl.translatesAutoresizingMaskIntoConstraints = false
    pageControl.currentPageIndicatorTintColor = .black
    pageControl.pageIndicatorTintColor = .systemGray2
    pageControl.numberOfPages = pages.count
    pageControl.currentPage = initialPage
    pageControl.isUserInteractionEnabled = false
}

func layout() {
    
    view.addSubview(pageControl)
    pageControl.snp.makeConstraints { make in
        make.width.equalToSuperview()
        make.height.equalTo(20)
        make.centerX.equalToSuperview()
    }
    pageControlBottomAnchor = view.bottomAnchor.constraint(equalToSystemSpacingBelow: pageControl.bottomAnchor, multiplier: 2)
    pageControlBottomAnchor?.isActive = true
}

@objc func pageControlTapped(_ sender: UIPageControl) {
    setViewControllers([pages[sender.currentPage]], direction: .forward, animated: true, completion: nil)
}
}
swift uipageviewcontroller
1个回答
0
投票

您应该在图像视图上将 clipsToBounds 设置为 true,这样图像视图的边界就不会被其内容超出。 (imageView.clipsToBounds = true)

© www.soinside.com 2019 - 2024. All rights reserved.