swift 如何在UIPageViewController中全屏显示图像?

l7wslrjt  于 2023-04-04  发布在  Swift
关注(0)|答案(1)|浏览(134)

我试图在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)
}
}
bzzcjhmw

bzzcjhmw1#

您应该在图像视图上将clipsToBounds设置为true,以便图像视图的内容不会超出其边界

相关问题