如何在iOS中的UIScrollView中添加子视图

时间:2016-01-19 09:45:48

标签: ios uiscrollview size

我想放置4个子视图,并且还想为整个视图设置scrollview。如何做到这一点? stateViewproviderView身高应为80。connectionNoView身高应为150,addTestNoView应为200。 但它只显示高达380高度。我已经展示了我的代码。

NewConnectionViewController.m

-(void)viewDidLoad
{

    [super viewDidLoad];

    //ScrollView
    self.view.backgroundColor = [UIColor grayColor];
    scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [scrollView setContentSize:CGSizeMake(self.view.frame.size.width,744)];
    [self.view addSubview:scrollView];

    //GettingView
    [self stateView];
    [self providerView];
    [self connectionNoView];
    [self addTestView];

}
-(void)stateView
{
    UIView *stateView = [[UIView alloc]initWithFrame:CGRectMake(0, self.navigationController.navigationBar.frame.size.height, self.view.frame.size.width, 80)];
    stateView.backgroundColor = [UIColor blueColor];
    [scrollView addSubview:stateView];

    //stateLabel
    UILabel *stateLabel = [[UILabel alloc]initWithFrame:CGRectMake(stateView.frame.origin.x+20,35, 70, 20)];
    [stateLabel setText:@"State"];
    [stateLabel setTextColor:[UIColor whiteColor]];
    [stateView addSubview:stateLabel];

    //TextField
    NSLog(@"stateViewSize==>>%f",stateView.frame.origin.y);
    stateText = [[UITextField alloc]initWithFrame:CGRectMake(stateView.frame.origin.x+110, 30, 150, 40)];
    stateText.textColor = [UIColor blackColor];
    stateText.backgroundColor = [UIColor clearColor];
    [stateText setBorderStyle:UITextBorderStyleRoundedRect];
    [stateText setPlaceholder:@"select State"];
    [stateView addSubview:stateText];
}

-(void)providerView
{
    UIView *providerView = [[UIView alloc]initWithFrame:CGRectMake(0, self.navigationController.navigationBar.frame.size.height+80, self.view.frame.size.width, 80)];
    providerView.backgroundColor = [UIColor orangeColor];
    [scrollView addSubview:providerView];
     NSLog(@"providerViewsize==>>%f",providerView.frame.origin.y);

    //providerLabel

    UILabel *providerLabel = [[UILabel alloc]initWithFrame:CGRectMake(providerView.frame.origin.x+20, 35, 70, 20)];
    [providerLabel setText:@"Provider"];
    [providerLabel setTextColor:[UIColor whiteColor]];
    [providerView addSubview:providerLabel];

    //TextField
    providerText = [[UITextField alloc]initWithFrame:CGRectMake(providerView.frame.origin.x+110,30, 150, 40)];
    providerText.textColor = [UIColor blackColor];
    [providerText setPlaceholder:@"select Provider"];
    providerText.backgroundColor = [UIColor clearColor];
    [providerText setBorderStyle:UITextBorderStyleRoundedRect];
    [providerView addSubview:providerText];
}

-(void)connectionNoView
{

    UIView *connectionNoView = [[UIView alloc]initWithFrame:CGRectMake(0, self.navigationController.navigationBar.frame.size.height+160, self.view.frame.size.width, 150)];
    connectionNoView.backgroundColor = [UIColor greenColor];
    [scrollView addSubview:connectionNoView];
    NSLog(@"connectionNoViewsize==>>%f",connectionNoView.frame.origin.y);

    //connectionNoTextField
    UITextField *connectionNoText=[[UITextField alloc]initWithFrame:CGRectMake(20, connectionNoView.frame.origin.y+30, 280, 35)];
    connectionNoText.borderStyle = UITextBorderStyleRoundedRect;
    [connectionNoText setPlaceholder:@"Add directly or use me ->"];
    connectionNoText.layer.borderColor = [[UIColor blackColor]CGColor];
    connectionNoText.delegate = self;
    [connectionNoView addSubview:connectionNoText];
}


-(void)addTestView
{
    UIView *addTestView = [[UIView alloc]initWithFrame:CGRectMake(0, self.navigationController.navigationBar.frame.size.height+310, self.view.frame.size.width, 200)];
    addTestView.backgroundColor = [UIColor blackColor];
    [scrollView addSubview:addTestView];
    NSLog(@"addTestViewSize==>>%f",addTestView.frame.origin.y);
}

1 个答案:

答案 0 :(得分:0)

您应该只为scrollView设置contentSize。

[scrollView setContentSize:CGSizeMake(self.view.frame.size.width, self.navigationController.navigationBar.frame.size.height + 510.0)];