martinjuhasz / MJPopupViewController

A UIViewController Category to display a ViewController as a popup with different transition effects.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

how to dynamic set size for PopupView

Kengi opened this issue · comments

whenever I try set popupViewController.view frame/bounds before presentPopupViewController or after it,
or try in popupViewcontrlloer

  • (void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    self.view.superview.bounds = CGRectMake(0, 0, 100, 100);
    }

all can not work, anyone can help me ?

commented

Hi @Kengi,
have you try to add a new method in UIViewController+MJPopupViewController.m like that :

  • (void)setNewPopupSize:(CGSize)size {
    UIView *popupView = [[self topView] viewWithTag:kMJPopupViewTag];
    [popupView setFrame:CGRectMake(10, 10, size.width, size.height)];
    }

I realize this thread is a bit old.

However I've trying to initialize the size of a dialog for different devices, I have a default size set in my view within interface builder, but I can't change this size.

@tybu I've tried using the code you suggest, however it doesn't work.

Can you provide me with some implementation code also?

Thanks in advance.

commented

hi @jules2010
i add this code in UIViewController+MJPopupViewController.m

#pragma mark -
#pragma mark Change Size

- (void)setNewPopupSize:(CGSize)size animated:(BOOL)animated {
    UIView *popupView = self.popupView;
    
    CGPoint point = [self getCenterPointFromSize:size];
    if (animated) {
        [UIView animateWithDuration:0.2f
                         animations:^ {
                             [popupView setFrame:CGRectMake(point.x, point.y, size.width, size.height)];
                         }
                         completion:^(BOOL finished) {
                             // to nothing
                         }
         ];
    } else {
        [popupView setFrame:CGRectMake(point.x, point.y, size.width, size.height)];
    }
}


- (CGPoint)getCenterPointFromSize:(CGSize)size {
    CGPoint newPoint;
    CGRect screenSize = [[UIScreen mainScreen] bounds];
    
    if(([[UIDevice currentDevice].systemVersion floatValue] >= 7.0 && [[UIDevice currentDevice].systemVersion floatValue] < 8.0)
       && UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) {
        newPoint.x = screenSize.size.height/2 - size.width/2;
        newPoint.y = screenSize.size.width/2 - size.height/2;
    }
    else {
        newPoint.x = screenSize.size.width/2 - size.width/2;
        newPoint.y = screenSize.size.height/2 - size.height/2;
    }
    
    return newPoint;
}

Thanks,

I'm not sure how that's different to the code I tried, apart from the center position...

This worked for me instead... Although I'm not sure if odd things happen when the dialog is closed, haven't tracked it down...

                svc.view.frame = CGRectMake(0, 0, size.width, size.height);

Hey @tybu

I'm revisiting this issue, where do you set self.popupView ?

I'm using UIView *popupView = [[self topView] viewWithTag:kMJPopupViewTag]; .

I've also replaced self.inferfaceOrientation in getCenterPointFromSize ...

    if(([[UIDevice currentDevice].systemVersion floatValue] >= 7.0 && [[UIDevice currentDevice].systemVersion floatValue] < 8.0)
       && self.view.bounds.size.width > self.view.bounds.size.height)

I'm getting rather odd results :(

Any help appreciated?

Thanks.

Jules.

commented

hey @jules2010,
indeed, it was not implicit.

i use a interface directly in UIViewController+MJPopupViewController.m

@interface MJPopupViewController()
@property(nonatomic, strong) UIView *sourceView;
@property(nonatomic, strong) UIView *overlayView;
@property(nonatomic, strong) UIView *backgroundView;
@property(nonatomic, strong) UIView *popupView;
@property(nonatomic) MJPopupViewAnimation popupAnimation;
@property(nonatomic, strong) UIViewController *parentVC;
@end

and transform

  • (void)presentPopupView:(UIView*)popupView animationType:(MJPopupViewAnimation)animationType dismissed:(void(^)(void))dismissed

into

  • (void)presentPopup:(MJPopupViewController*)popup animationType:(MJPopupViewAnimation)animationType
- (void)presentPopup:(MJPopupViewController*)popup animationType:(MJPopupViewAnimation)animationType
{
    UIView *sourceView = [self topView];
    
    popup.popupAnimation = animationType;
    popup.sourceView = sourceView;
    popup.popupView = popup.view;
    popup.popupView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin |UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin;
    
    // Add semi overlay
    popup.overlayView = [[UIView alloc] initWithFrame:sourceView.bounds];
    popup.overlayView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    popup.overlayView.backgroundColor = [UIColor clearColor];
    
    // BackgroundView
    popup.backgroundView = [[MJPopupBackgroundView alloc] initWithFrame:sourceView.bounds];
    popup.backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    popup.backgroundView.backgroundColor = [UIColor colorWithWhite:0. alpha:0.65];
    popup.backgroundView.alpha = 0.0f;
    [popup.overlayView addSubview:popup.backgroundView];
    
    // Make the Background Clickable
    UIButton * dismissButton = [UIButton buttonWithType:UIButtonTypeCustom];
    dismissButton.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    dismissButton.backgroundColor = [UIColor clearColor];
    dismissButton.frame = sourceView.bounds;
    [popup.overlayView addSubview:dismissButton];
    
    popup.popupView.alpha = 0.0f;
    [popup.overlayView addSubview:popup.popupView];
    [popup.sourceView addSubview:popup.overlayView];
    
    [dismissButton addTarget:popup action:@selector(dismissPopupViewControllerWithanimation:) forControlEvents:UIControlEventTouchUpInside];
    switch (animationType) {
        case MJPopupViewAnimationSlideBottomTop:
        case MJPopupViewAnimationSlideBottomBottom:
        case MJPopupViewAnimationSlideTopTop:
        case MJPopupViewAnimationSlideTopBottom:
        case MJPopupViewAnimationSlideLeftLeft:
        case MJPopupViewAnimationSlideLeftRight:
        case MJPopupViewAnimationSlideRightLeft:
        case MJPopupViewAnimationSlideRightRight:
            dismissButton.tag = animationType;
            [self slideViewIn:popup.popupView sourceView:popup.sourceView overlayView:popup.overlayView withAnimationType:animationType];
            break;
        default:
            dismissButton.tag = MJPopupViewAnimationFade;
            [self fadeViewIn:popup.popupView sourceView:popup.sourceView overlayView:popup.overlayView];
            break;
    }
}

hope this helps u :)

Thanks, however, I think something is missing, I'm getting Expected a type on the MJPopViewController* in the prestPopup method?

commented

i think you want to say presentPopup method...

have u change your UIViewController+MJPopupViewController.h ?
with - (void)presentPopupViewController:(MJPopupViewController*)popupViewController animationType:(MJPopupViewAnimation)animationType;

Ah yes that's what I mean. Yeah I tried adding it to the h file too, but it had the same issue.