简体   繁体   中英

Modify frame of the frontView in SWRevealViewController

Typically, if one is using SWRevealViewController, the front view fully covers the rear view, and the rear view is visible only if some action is performed (swipe, tap etc). My requirement is that the rear view should always be visible at least a little bit when the view loads and when the user swipes back. I tried modifying the frame of the frontView to achieve this, but I saw no effect whatsoever.

I put the frame update frame code in a method:

-(void)updateFrontViewControllerFrameWithPadding:(BOOL)bProvidePadding
{
    if (!bProvidePadding) {

        CGRect frontViewControllerFrame = self.frontViewController.view.bounds;

        CGFloat xOrigin = self.view.bounds.origin.x;
        CGFloat nWidth = self.view.bounds.size.width;

        frontViewControllerFrame.origin.x = xOrigin;
        frontViewControllerFrame.size.width = nWidth;

        self.frontViewController.view.frame = frontViewControllerFrame;

        _frontViewShadowColor = [UIColor blackColor];

        [_contentView reloadShadow];
    }

    else {

        CGFloat nPadding = 0.0f;

        if ([IILUtility targetDevice] == UIUserInterfaceIdiomPad) {

            _rearViewRevealWidthIpad = 20.0f;

            nPadding = _rearViewRevealWidthIpad;
        }
        else {

            _rearViewRevealWidthIphone = 15.0f;

            nPadding = _rearViewRevealWidthIphone;
        }

        CGRect revealViewBounds = self.view.bounds;

        CGFloat frontViewXPos = revealViewBounds.origin.x + nPadding;
        CGFloat frontViewWidth = revealViewBounds.size.width - nPadding;

        CGRect frontViewFrame = self.frontViewController.view.frame;

        frontViewFrame.origin.x = frontViewXPos;
        frontViewFrame.size.width = frontViewWidth;

        self.frontViewController.view.frame = frontViewFrame;

        _frontViewShadowColor = [UIColor colorWithWhite:0.5
                                                  alpha:0.5];

        [_contentView reloadShadow];

        //reset rearViewRevealWidth
        [self resetRearViewRevealWidth];

    }
}

This method is called from the viewWillLayoutSubviews method, with providePadding argument as TRUE. This method also needs to be called from places in SWRevealViewController where pan and tap gestures have been handled.

新边界

This is how the view looks when swiped:

刷卡

This is the solution to the frame resizing problem:

-(void)updateFrontViewControllerFrameWithPadding:(BOOL)bProvidePadding
{

    if (!bProvidePadding) {

        CGRect frontViewControllerFrame = self.frontViewController.view.bounds;

        CGFloat xOrigin = self.view.bounds.origin.x;
        CGFloat nWidth = self.view.bounds.size.width;

        frontViewControllerFrame.origin.x = xOrigin;
        frontViewControllerFrame.size.width = nWidth;

        self.frontViewController.view.frame = frontViewControllerFrame;

        _frontViewShadowColor = [UIColor blackColor];

        [_contentView reloadShadow];


        //There is a catch here. The shadow for _frontView is different from that of self.frontViewController.view
        //If we do not modify the shadow for self.frontViewController.view then it overlaps with
        // the shadow of _frontView. This is generally not a problem in the case when the rear view is in focus and the
        //front view is toggled to the right. But if we need the front view to be shifted slighlty to the right even when
        //its position is FrontViewPositionLeft, it is important to manage the shadow of self.frontViewController.view
        //otherwise the shadow takes up width equal to the padding we have provided.
        CALayer *frontViewLayer = self.frontViewController.view.layer;
        frontViewLayer.shadowColor = [UIColor colorWithWhite:0.8f
                                                       alpha:0.0f].CGColor;
        frontViewLayer.shadowOpacity = 0.0f;
        frontViewLayer.shadowOffset = CGSizeMake(0.0f, 0.0f);
        frontViewLayer.shadowRadius = 0.0f;

    }

    else {

        CGFloat nPadding = 0.0f;

        if ([IILUtility targetDevice] == UIUserInterfaceIdiomPad) {

            _rearViewRevealWidthIpad = 60.0f;

            nPadding = _rearViewRevealWidthIpad;
        }
        else {

            _rearViewRevealWidthIphone = 35.0f;

            nPadding = _rearViewRevealWidthIphone;
        }

        CGRect revealViewBounds = self.view.bounds;

        CGFloat frontViewXPos = revealViewBounds.origin.x + nPadding;
        CGFloat frontViewWidth = revealViewBounds.size.width - nPadding;

        CGRect frontViewFrame = self.frontViewController.view.frame;

        frontViewFrame.origin.x = frontViewXPos;
        frontViewFrame.size.width = frontViewWidth;

        self.frontViewController.view.frame = frontViewFrame;

        [self rearViewDeploymentForLeftPosition];

        _frontViewShadowColor = [UIColor colorWithWhite:0.8f
                                                  alpha:0.0f];

        [_contentView reloadShadow];

        //There is a catch here. The shadow for _frontView is different from that of self.frontViewController.view
        //If we do not modify the shadow for self.frontViewController.view then it overlaps with
        // the shadow of _frontView. This is generally not a problem in the case when the rear view is in focus and the
        //front view is toggled to the right. But if we need the front view to be shifted slighlty to the right even when
        //its position is FrontViewPositionLeft, it is important to manage the shadow of self.frontViewController.view
        //otherwise the shadow takes up width equal to the padding we have provided.
        CALayer *frontViewLayer = self.frontViewController.view.layer;
        frontViewLayer.shadowColor = [UIColor blackColor].CGColor;
        frontViewLayer.shadowOpacity = _frontViewShadowOpacity;
        frontViewLayer.shadowOffset = _frontViewShadowOffset;
        frontViewLayer.shadowRadius = _frontViewShadowRadius;

        //reset rearViewRevealWidth
        [self resetRearViewRevealWidth];

    }

}

This also takes into account the shadow related problem mentioned above. This method needs to be called from appropriate places, eg; when the view loads initially, when pan or tap gestures are performed etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM