2012-01-16 72 views
0

我在尝试将子视图添加到viewDidLoad上的UIScrollView时遇到问题。IOS在viewDidLoad上添加子视图

我使用此代码编程的UIImageViews的添加到滚动视图:

- (void)viewDidLoad { 
[super viewDidLoad]; 
NSInteger const_width = 100; 
NSInteger numberOfViews = 4; 
CGRect theFrame = [self.scrollView frame]; 
for (int i = 0; i < numberOfViews; i++) { 
    CGFloat xOrigin = i * const_width; 
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin,theFrame.origin.y,const_width,110)]; 
    UIImage *image = [UIImage imageNamed:@"cocoloco.jpg"]; 
    [imageView setImage:image]; 
    //[self.scrollView addSubview:imageView]; 
    imageView.tag = i; 
    CGRect rect = imageView.frame; 
    rect.size.height = 110; 
    rect.size.width = 110; 
    imageView.frame = rect; 
    [self.scrollView addSubview:imageView]; 

} 

self.scrollView.contentSize = CGSizeMake(const_width * numberOfViews, 110);} 

,但我得到的当前观点:

enter image description here

似乎滚动视图帧需要它的位置无论3个黄色选项卡(这是一个特殊的TabBarController),所以我从UIScrollView得到一个错误的框架起源,因此UIImageViews错误定位。

有什么想法?

回答

0

我不知道如何做到这一点,但将框架的高度添加到矩形的“Y”位置。事情是这样的:

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin,theFrame.origin.y + (theFrame.height),const_width,110)]; 
+0

但问题是UIScrollView的框架没有考虑到它在tabBarController(导航栏下方的3个黄色标签)内。谢谢:) – xger86x 2012-01-16 16:08:37

0

要获得在顶部导航栏下方的滚动视图,尝试

self.scrollview.autoresizingMask = UIViewAutoresizingFlexibleTopMargin; 

然后通过设置它在滚动视图起源和高度产地基本位置滚动视图下表:

CGRect frame = tableView.frame; 
frame.origin.y = self.scrollview.frame.origin.y + self.scrollview.frame.size.height; 
tableView.frame = frame; 
+0

不,我需要黄色标签下面的scrollview ... – xger86x 2012-01-16 19:16:12

+0

别担心我终于找到了解决方案 – xger86x 2012-01-27 18:10:26

0

你应该将你的UI到任何大小调整或布局 - (空)layoutSubviews方法,这应该正确地排序您的问题。