简体   繁体   English

将页面控件设置为UIPageViewController的顶部

[英]Set page control to the top of UIPageViewController

Currently i want to use UIPageViewController. 目前,我想使用UIPageViewController。 But by default the page control is automatic place on the bottom of screen. 但是默认情况下,页面控件是自动放置在屏幕底部的。 so how can i achieve like the image below where the page control is on the top 所以我怎么能像下面的图像一样实现页面控件在顶部

在此处输入图片说明

i use container view and change the view controller to uipageviewcontroller 我使用容器视图并将视图控制器更改为uipageviewcontroller

UPDATE 更新

class ProfilePageViewController: UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource {

var pageControl = UIPageControl(frame: CGRect(x: 0,y: 0,width: UIScreen.main.bounds.width,height: 50))
lazy var VCArr: [UIViewController] = {
    return [self.VCInstance(name: "button1"),
            self.VCInstance(name: "button2"),
            self.VCInstance(name: "button3")]
}()

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    for view in self.view.subviews {
        if view is UIScrollView {
            view.frame = UIScreen.main.bounds
        } else if view is UIPageControl {
            view.backgroundColor = UIColor.clear
        }
    }
}

private func VCInstance(name: String) -> UIViewController {
    return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: name)
}

override func viewDidLoad() {
    super.viewDidLoad()
    self.dataSource = self
    self.delegate = self
    pageControl.currentPageIndicatorTintColor = UIColor.green
    pageControl.pageIndicatorTintColor = UIColor.gray
    if let firstVC = VCArr.first {
        setViewControllers([firstVC], direction: .forward, animated: true, completion: nil)
    }
}



public func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
    guard let viewControllerIndex = VCArr.index(of: viewController) else {
        return nil
    }

    let previousIndex = viewControllerIndex - 1

    guard previousIndex >= 0 else {
        return VCArr.last
        //uncomment atas comment bawa untuk loop
        //return nil
    }

    guard VCArr.count > previousIndex else {
        return nil
    }

    return VCArr[previousIndex]

}

public func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
    guard let viewControllerIndex = VCArr.index(of: viewController) else {
        return nil
    }

    let nextIndex = viewControllerIndex + 1

    guard nextIndex < VCArr.count else {
        return VCArr.first
        //uncomment atas, comment bawah untuk loop
        //return nil
    }

    guard VCArr.count > nextIndex else {
        return nil
    }

    return VCArr[nextIndex]

}

public func presentationCount(for pageViewController: UIPageViewController) -> Int {
    return VCArr.count
}


public func presentationIndex(for pageViewController: UIPageViewController) -> Int {
    guard let firstViewController = viewControllers?.first,
        let firstViewControllerIndex = VCArr.index(of: firstViewController) else {
            return 0
    }

    return firstViewControllerIndex
}


}

i have already updated the latest code what i had already did, but the page control seem still at the bottom of page. 我已经更新了最新的代码,但是页面控件似乎仍在页面底部。

below is the image of my current page control 以下是我当前页面控件的图像

在此处输入图片说明

Don't use the default UIPageControl from appearance. 不要从外观上使用默认的UIPageControl。 Remove those block. 删除那些块。 Create a new UIPageControl based on your view rect and position. 根据您的视图矩形和位置创建一个新的UIPageControl。

In frame of UIpagecontrol: set y = 0 means top or UIScreen.main.bounds.maxY - offset 在UIpagecontrol的框架中:设置y = 0表示top或UIScreen.main.bounds.maxY-偏移量

Here is the sample code 这是示例代码

 //1 : member variable
 var pageControl = UIPageControl()

// Call it from viewDidLoad
func setupPageControl() {
   // 2: set y = 0 means top or UIScreen.main.bounds.maxY - offset
    pageControl = UIPageControl(frame: CGRect(x: 0,y: 0,width: UIScreen.main.bounds.width,height: 50))
    self.pageControl.numberOfPages = pageCount
   // self.pageControl.currentPage = 0
    self.pageControl.tintColor = UIColor.lightGray
    self.pageControl.pageIndicatorTintColor = UIColor.lightGray
    self.pageControl.currentPageIndicatorTintColor = UIColor.black
    pageControl.backgroundColor = UIColor.clear
    self.view.addSubview(pageControl)
}

You need to update the current page ( pageControl.currentPage = count ) from viewControllerAfter () & viewControllerBefore 您需要从viewControllerAfter ()和viewControllerBefore更新当前页面( pageControl.currentPage = count

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在几秒钟内重新加载UiPageViewController - Reload UiPageViewController in seconds Swift-如何将分段控件锁定在屏幕顶部 - Swift - How to get Segmented control to lock at top of screen 在UIPageViewController中调整实例化视图控制器的大小? - Sizing instantantiated view controllers in a UIPageViewController? 是否可以将分段控制标题的 alignment 设置在左侧? - Is it possible to set the alignment of segmented Control titles to the left? 使用情节提要板将UIl​​abel的顶部设置为屏幕大小的一部分 - set top of UIlabel as fraction of the screen size using storyboard 在已设置的图像上加载图像的问题swift 4 - problem with images loading on top of already set images swift 4 如何使 UIPageviewcontroller 像幻灯片图像查看器一样 - How to make UIPageviewcontroller like a slide image viewer 如何使用 NSTimer 在 swift4 中使用页面控件进行自动滚动 - How to work with Page control using NSTimer for automatic scrolling in swift4 Swift 4 UIPageViewController没有显示正确的视图(按顺序在数组中定义) - Swift 4 UIPageViewController not showing the correct views (as defined in an array in sequence 嵌入到NavigationController swift4中时,从UIPageViewController更新ChildViewController - Update ChildViewController from UIPageViewController when embeded in NavigationController swift4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM