2012-12-27 43 views
2

我的iPad应用程序中有UIScrollView和UIPageControl。我已将它们放入.xib文件中。我的应用仅支持横向视图。如何在iPad中正确设置UIPageControl大小

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

所以我的问题是:

  1. 我怎样才能在代码中设置大小合适的(viewDidLoad中?)?
  2. 我是否需要为UIScrollView和UIPageControl设置大小?

在此先感谢。

回答

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]; 
+0

很好的答案。谢谢。 –

0

当你使用xib的时候,你为什么要用代码来设置宽度和高度。而且你面临的问题我认为是因为autoresizing只是试图通过修改这个希望它会起作用。

+0

无论我如何设置宽度和高度,在IB或代码中,它都不起作用。 Autoresizing没有修复它:( –

相关问题