UIView子视图不会显示在UIScrollView中

时间:2011-06-19 19:14:36

标签: iphone ios uiview uiscrollview subview

我是iOS开发的新手,我正在尝试制作像Engadget的iPhone应用程序的滚动标题,我创建了一个自定义UIView作为UIScrollView的子视图,放置子视图的算法似乎没问题因为我可以滚动子视图,以编程方式改变视图的背景颜色,但问题是我看不到任何内容,只是灰色背景我做错了什么?

提前致谢。

这是我的代码:

Promoted是一个带有两个标签和UIImageView的UIView。

- (void)layoutScrollSubViews
{
promoted *view = nil;
NSArray *subviews = [sView1 subviews];

// reposition all image subviews in a horizontal serial fashion
CGFloat curXLoc = 0;
for (view in subviews)
{
    if ([view isKindOfClass:[promoted class]]  && view.tag > 0)
    {
        CGRect frame = view.frame;
        frame.origin = CGPointMake(curXLoc, 0);
        frame.size.height = kScrollObjHeight;
        frame.size.width = kScrollObjWidth;
        view.frame = frame;

        curXLoc += (kScrollObjWidth);
    }
}

// set the content size so it can be scrollable
[sView1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth), [sView1 bounds].size.height)];

}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad{    
NSUInteger i;
for (i = 1; i <= kNumImages; i++)
{

    promoted *p = [[promoted alloc] initWithFrame:CGRectMake(0, 0, 320, 187)];

    p.title.text = @"Test 1";
    p.num.text = @"1/1";
    p.num.backgroundColor = [UIColor whiteColor];
    // setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
    p.backgroundColor = [UIColor colorWithRed:(.9/i) green:(.9/i) blue:(.9/i) alpha:1];

    p.tag = i;  // tag our images for later use when we place them in serial fashion

    [sView1 addSubview:p];
    [p release];
}

[self layoutScrollSubViews];
[super viewDidLoad];
}

2 个答案:

答案 0 :(得分:0)

你可以做两件事: 1.将图片视图和标签直接添加到scrollView 2.在将标签和ImageView添加到UIView之前,还要分配标签和ImageView(并且在要求完成时,不要忘记在相应的范围内发布它们)

答案 1 :(得分:0)

根据我的经验,UIScrollView是Apple发布的奇怪的bug 控件。通过nib文件添加时不知道它为什么会出现异常。我总是尝试以编程方式创建UIScrollView并通过以编程方式分配它们来添加子视图。

可能听起来很奇怪,但它大部分时间都适合我!