如何在iOS中调整子视图高度

时间:2014-07-25 07:22:30

标签: objective-c uiview uiscrollview uiimageview subview

我有2个子视图A和B.Both在ScrollView中放置。在ScrollView中首先放置A然后放置B.A子视图包含uiimageview。 B子视图只包含UitextField。在子视图中我可以为Gallery.using UIImagePickerController上传图像。我需要的是在uiimageView上传图像之前无需显示" A " subview.B首先出现在UIScrollView.if上传图片中。子视图首先出现在uiimageView上,然后放置在B子视图中。

       - (void)viewDidLoad
    {
        [super viewDidLoad];
        scrol=[[UIScrollView alloc]init];
        scrol.frame=CGRectMake(0, 100, self.view.frame.size.width, 250);
        scrol.delegate = self;
        scrol.scrollEnabled = YES;
        [self.view addSubview:scrol];
        scrol.contentSize =CGSizeMake(221, 650);
        scrol.backgroundColor=[UIColor blueColor];

        A=[[UIView alloc]init];
        A.frame=CGRectMake(10, 10, 221, 100);
        A.backgroundColor=[UIColor yellowColor];
        [scrol addSubview:A];

        B=[[UIView alloc]init];
        B.frame=CGRectMake(10, 120, 300, 100);
        B.backgroundColor=[UIColor grayColor];
        [scrol addSubview:B];


        firstimage1=[[UIImageView alloc]init];
        firstimage1.frame=CGRectMake(5, 10, 180, 80);
        firstimage1.backgroundColor=[UIColor redColor];
      //firstimage1.image = [UIImage imageNamed:@"Defult.png"];
        [A addSubview:firstimage1];

        UITextField *field1=[[UITextField alloc]init];
        field1.frame=CGRectMake(10, 10, 221, 30);
        field1.backgroundColor=[UIColor whiteColor];
        [B addSubview:field1];

        UITextField *field2=[[UITextField alloc]init];
        field2.frame=CGRectMake(10, 50, 221, 30);
        field2.backgroundColor=[UIColor whiteColor];
        [B addSubview:field2];

       getImages=[[UIButton alloc]init];
        getImages.frame=CGRectMake(0, 0, 28, 25);
        [self.view addSubview:getImages];
        [getImages addTarget:self action:@selector(aImages:) forControlEvents:UIControlEventTouchDown];
        [getImages setBackgroundImage:[UIImage imageNamed:@"Defult.png"] forState:UIControlStateNormal];


        img1 = [[UIImagePickerController alloc] init];
        img1.delegate = self;
        img1.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        img1.delegate = self;



        if(firstimage1.image){
            firstimage1.hidden=NO;
            A.hidden=NO;
            //if Image is there just set the A_View as your need.
            [A setFrame:CGRectMake(10, 10, 221, 100)];
            //and set B_View as per the Y co ordinate + height of A_View
            [B setFrame:CGRectMake(0, A.frame.origin.y+A.frame.size.height+10, 300, 100)];


        }

        else{
            firstimage1.hidden=YES;
            //And if image not there make it  hidden then make Aview hide and put your Bview at place of AView
            A.hidden=YES;
            [B setFrame:CGRectMake(10, 10, 300, 100)];
        }



    }
-(void)aImages:(id)sender
{

    actionSheet11 = [[UIActionSheet alloc]
                     initWithTitle:@"pick phot"
                     delegate:self
                     cancelButtonTitle:@"back"
                     destructiveButtonTitle:nil
                     otherButtonTitles:@"gallery", @"camera",
                     nil];
    actionSheet11.actionSheetStyle = UIActionSheetStyleBlackTranslucent;

    [actionSheet11 showInView:self.view];

}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    img1.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    int i = buttonIndex;
    switch (i) {
        case 0:
            img1.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            [self presentViewController:img1 animated:YES completion:^{}];
            break;
        case 1:
            img1.sourceType = UIImagePickerControllerSourceTypeCamera;
            [self presentViewController:img1 animated:YES completion:^{}];
            break;
        default:
            actionSheet11.hidden=YES;


    }
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissModalViewControllerAnimated:YES];
    firstimage1.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];


}

在加载图像之前不需要显示A子视图,B首先出现在UIScrollView中。使用UIImagePickerController加载imageView后首先出现B然后放置B。我的代码是在图像加载之前它的B先出现。但是在上传时image现在显示的子视图

请帮助我谢谢高级

2 个答案:

答案 0 :(得分:1)

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{

firstimage1.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

 if(firstimage1.image){
        firstimage1.hidden=NO;
        A.hidden=NO;
        //if Image is there just set the A_View as your need.
        [A setFrame:CGRectMake(10, 10, 221, 100)];
        //and set B_View as per the Y co ordinate + height of A_View
        [B setFrame:CGRectMake(0, A.frame.origin.y+A.frame.size.height+10, 300, 100)];
  [picker dismissModalViewControllerAnimated:YES];

    }

    else{
        firstimage1.hidden=YES;
        //And if image not there make it  hidden then make Aview hide and put your Bview at place of AView
        A.hidden=YES;
        [B setFrame:CGRectMake(10, 10, 300, 100)];
    }

}

答案 1 :(得分:0)

请执行以下操作,如果隐藏了

,则应将B视图放在A视图的位置
     if(myimageView.image){
    myimageview.hidden=NO;
    A.hidden=NO;
        //if Image is there just set the A_View as your need.
            [A setFrame:CGRectMake(0, 10, 200, 200)];
        //and set B_View as per the Y co ordinate + height of A_View
            [B setFrame:CGRectMake(0, AView.frame.origin.y+AView.frame.size.height+Custom_Padding_space, 200, 200)];


        }

        else{
    myimageView.hidden=YES;
    //And if image not there make it  hidden then make Aview hide and put your Bview at place of AView
            A.hidden=YES;
            [B setFrame:CGRectMake(0, 10, 200, 200)];
}

这将起作用:你需要使用枚举值而不是传递为字符串参数。

- (void)imagePickerController:(UIImagePickerController *)picker 
                      didFinishPickingMediaWithInfo:(NSDictionary *)info {

  [picker dismissModalViewControllerAnimated:YES];


  firstimage1.image = [info objectForKey:UIImagePickerControllerOriginalImage];

}