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

后端 未结 7 1766
闹比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:07

    The same effect can be achieved simply by subclassing UIPageViewController and overriding viewDidLayoutSubviews as follows:

    -(void)viewDidLayoutSubviews {
        UIView* v = self.view;
        NSArray* subviews = v.subviews;
        if( [subviews count] == 2 ) {
            UIScrollView* sv = nil;
            UIPageControl* pc = nil;
            for( UIView* t in subviews ) {
                if( [t isKindOfClass:[UIScrollView class]] ) {
                    sv = (UIScrollView*)t;
                } else if( [t isKindOfClass:[UIPageControl class]] ) {
                    pc = (UIPageControl*)t;
                }
            }
            if( sv != nil && pc != nil ) {
                // expand scroll view to fit entire view
                sv.frame = v.bounds;
                // put page control in front
                [v bringSubviewToFront:pc];
            }
        }
        [super viewDidLayoutSubviews];
    }
    

    Then there is no need to maintain a seperate UIPageControl and such.

提交回复
热议问题