是否可以在UIScrollView上方放置UIPageControl?

时间:2015-03-10 11:08:35

标签: ios iphone xcode

您好我整天都在绞尽脑汁。我有大约5个月的iOS经验。我要做的是将UIPageControl视图放在UIScrollView上方。这是我的代码......

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    //self.scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,[self window_width], [self window_height])];
    [self.scrollView setContentSize:CGSizeMake([self window_width], [self window_height])];
    [self.scrollView setPagingEnabled:YES];
    [self.scrollView setScrollEnabled:YES];
    [self.scrollView setShowsHorizontalScrollIndicator:NO];
    [self.scrollView setShowsVerticalScrollIndicator:NO];
    [self.scrollView setDelegate:self];

    self.pageControl = [[FXPageControl alloc] initWithFrame:CGRectMake(0, 0, [self window_width], [self window_height])];
    self.pageControl.dotShape = FXPageControlDotShapeCircle;
    self.pageControl.selectedDotShape = FXPageControlDotShapeCircle;
    self.pageControl.numberOfPages = [self.childViewControllers count];
    self.pageControl.selectedDotSize = 30.0;
    self.pageControl.dotSize = 30.0;
    self.pageControl.defersCurrentPageDisplay = YES;
    self.pageControl.dotSpacing = 50.0;
    self.pageControl.backgroundColor = [UIColor whiteColor];

    [self.view addSubview:self.pageControl];
    [self.view addSubview:self.scrollView];
}

我尝试交换SubViews ScrollView pageControl而不是scrollview,这让{{1}}消失了。

3 个答案:

答案 0 :(得分:0)

您可以分别为视图和滚动视图划分视图的空间,或者您可以在滚动视图中添加网页浏览控制器并调整滚动视图的内容高度。

你们彼此重叠视线。

答案 1 :(得分:0)

您可以通过以下代码将UIScrollView上方的UIPageControl添加到self.view

- (void)viewDidLoad {
 [super viewDidLoad];
 // Do any additional setup after loading the view from its nib.
 //self.scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
 self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, [self window_width], [self window_height])];
 [self.scrollView setContentSize:CGSizeMake([self window_width], [self window_height])];
  [self.scrollView setPagingEnabled:YES];
  [self.scrollView setScrollEnabled:YES];
  [self.scrollView setShowsHorizontalScrollIndicator:NO];
  [self.scrollView setShowsVerticalScrollIndicator:NO];
  [self.scrollView setDelegate:self];

  self.pageControl = [[FXPageControl alloc] initWithFrame:CGRectMake(0, 0, [self window_width], [self window_height])];
  self.pageControl.dotShape = FXPageControlDotShapeCircle;
  self.pageControl.selectedDotShape = FXPageControlDotShapeCircle;
  self.pageControl.numberOfPages = [self.childViewControllers count];
  self.pageControl.selectedDotSize = 30.0;
  self.pageControl.dotSize = 30.0;
  self.pageControl.defersCurrentPageDisplay = YES;
  self.pageControl.dotSpacing = 50.0;
  self.pageControl.backgroundColor = [UIColor whiteColor];

  [self.view addSubview:self.scrollView];
  [self.view addSubview:self.pageControl];
}

此代码将UIScrollView添加到self.view,然后将UIPageControl添加到self.view

或者你可以添加

  

[self.view bringSubviewToFront:self.pageContol];

答案 2 :(得分:0)

我做到了。我不知道这是不是很好的做法。无论如何,下面的代码将UIPageControl放在UIScrollView上。

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    [self.scrollView setPagingEnabled:YES];
    [self.scrollView setScrollEnabled:YES];
    [self.scrollView setShowsHorizontalScrollIndicator:NO];
    [self.scrollView setShowsVerticalScrollIndicator:NO];
    [self.scrollView setDelegate:self];
    [self.scrollView setNeedsDisplay];

    self.pageControl = [[FXPageControl alloc] initWithFrame:CGRectMake(0, 90, [self window_width], 50)];
    self.pageControl.dotShape = FXPageControlDotShapeCircle;
    self.pageControl.selectedDotShape = FXPageControlDotShapeCircle;
    self.pageControl.numberOfPages = [self.childViewControllers count];
    self.pageControl.selectedDotSize = 30.0;
    self.pageControl.dotSize = 30.0;
    self.pageControl.defersCurrentPageDisplay = YES;
    self.pageControl.dotSpacing = 50.0;
    self.pageControl.backgroundColor = [UIColor whiteColor];
    self.pageControl.selectedDotColor = UIColorFromRGB(0x54b97f);
    self.pageControl.dotColor = [UIColor darkGrayColor];
    [self.view addSubview:self.pageControl];
    [self.view addSubview:self.scrollView];
}