Add UIView as subView on UIActionSheet

2019-01-29 02:23发布

问题:

I am working on project where I want to show UIActionsheet with three buttons Change,Cancel and Done and at the place of title I want to put UIView which has some labels. I have created dynamic UIView and I added it to actionsheet but its hiding behind buttons I tried it by all possible ways to setFrame,bounds but I am not able to find solution. I want to place this UIView at the top of UIActionsheet followed by buttons. If you have any doubts feel free to post comments.

Here is My Code:

-(IBAction)tapbutton:(id)sender
{
Lablesubview *view = [[Lablesubview alloc]init];
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
                                               delegate:self 
                                      cancelButtonTitle:@"Cancel" 
                                 destructiveButtonTitle:@"Change" 
                                      otherButtonTitles:@"Done",nil];

   [actionSheet addSubview:view];
   [actionSheet showInView:self.view];
}

回答1:

You can use this method, it will move all the buttons down by 100 pixels, please note that this type of modificaiton may result in your application rejection

-(IBAction)tapbutton:(id)sender
{   
    //create the view
    Lablesubview *view = [[Lablesubview alloc]init];
    //Set the frame
    view.frame = CGRectMake(0, 10, 320, 100);
    view.backgroundColor = [UIColor greenColor];

    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
                                                             delegate:self 
                                                    cancelButtonTitle:@"Cancel" 
                                               destructiveButtonTitle:@"Change" 
                                                    otherButtonTitles:@"Done",nil];

    [actionSheet showInView:self.view];

    CGRect rect;

    //expand the action sheet
    rect = actionSheet.frame;
    rect.size.height +=100;
    rect.origin.y -= 100;
    actionSheet.frame = rect;

    //Displace all buttons
    for (UIView *vButton in actionSheet.subviews) {
        rect = vButton.frame;
        rect.origin.y += 100;
        vButton.frame = rect;
    }    


    //Add the new view
    [actionSheet addSubview:view];
}