代码之家  ›  专栏  ›  技术社区  ›  user3839375

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

  •  0
  • user3839375  · 技术社区  · 10 年前

    我有2个子视图A和B。这两个子视图都位于ScrollView中。在ScrollView,首先放置A,然后放置B。子视图包含uiimageview。B子视图仅包含UitextField。在A子视图中,我可以使用UIImagePickerController为Gallery上载图像。我需要在uiimageView上上载图像之前无需显示“A “subview.B首先出现在UIScrollView.if上传图像上。subview首先出现在uiimageView上,然后放置B subview。

           - (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"];
    
    
    }
    

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

    请帮我谢谢高级

    2 回复  |  直到 10 年前
        1
  •  1
  •   Anbu.Karthik    10 年前
    -(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)];
        }
    
    }
    
        2
  •  0
  •   BhavikKama    10 年前

    请执行以下操作:如果A视图隐藏,则应将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)];
    }
    

    这将起作用:您需要使用Enum Value For,而不是作为字符串参数传递。

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