2

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

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

所以我的问题是:

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

提前致谢。

4

2 回答 2

0

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

于 2012-12-28T00:48:14.133 回答
0

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

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];
于 2012-12-28T05:37:43.213 回答