iOS 7-UIScrollView子视图未显示

iOS 7-UIScrollView子视图未显示,ios,iphone,uiview,uiscrollview,Ios,Iphone,Uiview,Uiscrollview,正在尝试将子视图添加到UIScrollView,该视图在XIB文件中定义。下面的代码是我如何尝试在视图控制器中添加子视图的。但没有添加子视图 有什么建议吗 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. CGRect frame = _tasksScrollView.frame; frame.size

正在尝试将子视图添加到UIScrollView,该视图在XIB文件中定义。下面的代码是我如何尝试在视图控制器中添加子视图的。但没有添加子视图

有什么建议吗

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    CGRect frame = _tasksScrollView.frame;
    frame.size.width /= 3.0;

    NSArray *colors = [NSArray arrayWithObjects:[UIColor blueColor],
                                                [UIColor redColor],
                                                [UIColor yellowColor],
                                                [UIColor greenColor],
                                                [UIColor purpleColor],
                                                nil];

    for (int i = 0; i < [colors count]; i++, frame.origin.x += frame.size.width) {
        UIView *subView = [[UIView alloc] initWithFrame:frame];
        subView.backgroundColor = [colors objectAtIndex:i];

        [_tasksScrollView addSubview:subView];
    }

    _tasksScrollView.contentSize = CGSizeMake(frame.size.width * [colors count], frame.size.height);
}
-(void)viewDidLoad
{
[超级视图下载];
//从nib加载视图后,执行任何其他设置。
CGRect frame=\u taskscrollview.frame;
框架尺寸宽度/=3.0;
NSArray*colors=[NSArray arrayWithObjects:[UIColor blueColor],
[UIColor redColor],
[UIColor yellowColor],
[UIColor绿色],
[UIColor紫色],
零];
对于(int i=0;i<[colors count];i++,frame.origin.x++=frame.size.width){
UIView*子视图=[[UIView alloc]initWithFrame:frame];
subView.backgroundColor=[colors objectAtIndex:i];
[\u taskscrollview addSubview:subView];
}
_taskscrollview.contentSize=CGSizeMake(frame.size.width*[colors count],frame.size.height);
}

可能是您的子视图相对于滚动视图中的视图空间偏移,因为您从
scrollview.frame
本身携带了
x
y
坐标,而该坐标实际上是相对于全屏的位置。尝试设置子视图的
x
y
位置,使其从
0

发生这种情况的原因有很多,大多数情况下,您只是无法链接/连接XIB中的IBOutlet属性(_taskscrollView)

您确定frame.origin.y为0可能是您的子视图相对于滚动视图中的视图空间偏移,因为您从
scrollview.frame
本身携带
x
y
坐标,而这实际上是相对于全屏的位置。尝试设置子视图的
x
y
位置,使其从
0
开始。感谢您的帮助!我的UIScrollView不是从原点(0,0)开始的,我将子视图框架的原点设置为与UIScrollView相同,这不起作用。将子视图的原点设置为(0,0)有效。@bohanl很高兴您将其修复。