how to add close button to modal view corner which is presented in UIModalPresentationPageSheet?

前端 未结 2 1745
野性不改
野性不改 2021-01-06 04:58

I want to add a floating close (x) button at the corner of an UIModalPresentationPageSheet View. The effect is like below:

2条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-06 05:43

    You can try and add it to the the topmost application window:

        add.modalPresentationStyle = UIModalPresentationPageSheet;
        [self presentViewController:add animated:YES completion:^{
            [[[[UIApplication sharedApplication] windows] lastObject] addSubview:button];
        }];
    

    This should give you the right view to allow the button to appear on top of the modal and receive touch events.

    --edit--

    To position the button you can try something like this:

    self.modalPresentationStyle = UIModalPresentationFormSheet;
    add.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentViewController:pvc animated:YES completion:^{
        CGRect parentView = pvc.view.superview.frame;
        CGRect buttonFrame = button.frame;
        buttonFrame.origin = CGPointMake(parentView.origin.x - buttonFrame.size.width/2.0f, parentView.origin.y - buttonFrame.size.height/2.0f);
    
        [button setFrame:buttonFrame];
        [[[[UIApplication sharedApplication] windows] lastObject] addSubview:button];
    }];
    

    This will get the frame of the modalView that has been presented. You can then set your button's origin to be offset and set the adjusted frame.

提交回复
热议问题