【问题标题】:How to set UIPageControl size correctly in iPad如何在 iPad 中正确设置 UIPageControl 大小
【发布时间】:2012-12-27 23:03:54
【问题描述】:

我的 iPad 应用程序中有一个 UIScrollView 和一个 UIPageControl。我已将它们放在 .xib 文件中。我的应用仅支持横向视图。

我尝试在代码中设置宽度和高度,但它看起来总是不对。我有一个非常简单的案例,将 5 个页面设置为不同的颜色背景,以确保我的页面看起来正确,但它们从来没有,无论我做什么,颜色都会重叠。

所以我的问题是:

  1. 如何在代码中正确设置大小(viewDidLoad?)?
  2. 是否需要同时设置 UIScrollView 和 UIPageControl 的大小?

提前致谢。

【问题讨论】:

    标签: uiscrollview uipagecontrol


    【解决方案1】:

    请检查下面的代码。在这里,我已经使用代码完成了所有工作。希望对你有帮助。

    int numberOfPages = 5 ;
    
    UIScrollView *scrollQuestionList = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, self.view.frame.size.height - 70.0)];
    scrollQuestionList.contentSize = CGSizeMake(numberOfPages*scrollQuestionList.frame.size.width, scrollQuestionList.frame.size.height);
    [self.view addSubview:scrollQuestionList];
    scrollQuestionList.backgroundColor = [UIColor clearColor];
    scrollQuestionList.delegate = self;
    scrollQuestionList.showsHorizontalScrollIndicator = NO;
    scrollQuestionList.pagingEnabled = YES;
    
    CGFloat x = 0.0;
    
    tagForBtns = 0;
    
    for (int i = 1; i <= numberOfPages; i++) 
    {
        UIView *contentView = [[[UIView alloc] initWithFrame:CGRectMake(x, 0.0, scrollQuestionList.frame.size.width, scrollQuestionList.frame.size.height)] autorelease];
    
        if (i%2 == 0)
        {
            [contentView setBackgroundColor:[UIColor grayColor]];
        }
        else
        {
            [contentView setBackgroundColor:[UIColor cyanColor]];
        }
    
        [scrollQuestionList addSubview:contentView];
        x+= scrollQuestionList.frame.size.width;
    }
    UIPageControl *pageCntrl = [[UIPageControl alloc] initWithFrame:CGRectMake(0.0, self.view.frame.size.height - 70.0, self.view.frame.size.width, 25.0)];
    pageCntrl.numberOfPages = numberOfPages;
    pageCntrl.backgroundColor = [UIColor clearColor];
    [pageCntrl addTarget:self action:@selector(clickedPageControl:) forControlEvents:UIControlEventValueChanged];
    pageCntrl.currentPage = 0;    
    [self.view addSubview:pageCntrl];
    

    【讨论】:

      【解决方案2】:

      当您使用 xib 时,为什么要使用代码设置宽度和高度。还有我认为由于自动调整大小而面临的问题,只需尝试修改这个希望它会起作用。

      【讨论】:

      • it' 不管我如何在 IB 或代码中设置宽度和高度,它仍然不起作用。自动调整大小也没有解决它:(
      猜你喜欢
      • 2018-12-28
      • 1970-01-01
      • 1970-01-01
      • 2014-03-22
      • 1970-01-01
      • 2011-10-11
      • 1970-01-01
      • 2023-03-25
      相关资源
      最近更新 更多