How to put the UIPageControl element on top of the sliding pages within a UIPageViewController?

后端 未结 7 1783
闹比i
闹比i 2020-12-02 10:20

Regarding to this tutorial by AppCoda about how to implement a app with UIPageViewController I\'d like to use a custom page control element on top of the pages instead of at

7条回答
  •  无人及你
    2020-12-02 11:02

    Here's the Swifty 2 answer very much based on @zerotool's answer above. Just subclass UIPageViewController and then add this override to find the scrollview and resize it. Then grab the page control and move it to the top of everything else. You also need to set the page controls background color to clear. Those last two lines could go in your app delegate.

    override func viewDidLayoutSubviews() {
    
        super.viewDidLayoutSubviews()
    
        var sv:UIScrollView?
        var pc:UIPageControl?
    
        for v in self.view.subviews{
    
            if v.isKindOfClass(UIScrollView) {
    
                sv = v as? UIScrollView
    
            }else if v.isKindOfClass(UIPageControl) {
    
                pc = v as? UIPageControl
            }
        }
    
        if let newSv = sv {
    
            newSv.frame = self.view.bounds
        }
    
        if let newPc = pc {
    
            self.view.bringSubviewToFront(newPc)
        }
    }
    
    let pageControlAppearance = UIPageControl.appearance()
    pageControlAppearance.backgroundColor = UIColor.clearColor()
    

    btw - I'm not noticing any infinite loops, as mentioned above.

提交回复
热议问题