0

I have already ask question about my problem Here.But still din't get solution .this is my UIViewController+ENPopUp.m

#import "UIViewController+ENPopUp.h"
#import "JWBlurView.h"
#import <objc/runtime.h>

static void * ENPopupViewControllerPropertyKey = &ENPopupViewControllerPropertyKey;

static CGFloat const kAnimationDuration = .4f;
static CGFloat const kRotationAngle = 70.f;

static NSInteger const kENPopUpOverlayViewTag   = 351301;
static NSInteger const kENPopUpViewTag          = 351302;
static NSInteger const kENPopUpBluredViewTag    = 351303;

@implementation UIViewController (ENPopUp)

#pragma mark - Public Methods
- (void)presentPopUpViewController:(UIViewController *)popupViewController
{
    [self presentPopUpViewController:popupViewController completion:nil];
}

- (void)presentPopUpViewController:(UIViewController *)popupViewController completion:(void (^)(void))completionBlock
{
    self.en_popupViewController = popupViewController;
    [self presentPopUpView:popupViewController.view completion:completionBlock];
}

- (void)dismissPopUpViewController
{
    [self dismissPopUpViewControllerWithcompletion:nil];
}

- (void)dismissPopUpViewControllerWithcompletion:(void (^)(void))completionBlock
{
    UIView *sourceView = [self topView];
    JWBlurView *blurView = (JWBlurView *)[sourceView viewWithTag:kENPopUpBluredViewTag];
    UIView *popupView = [sourceView viewWithTag:kENPopUpViewTag];
    UIView *overlayView = [sourceView viewWithTag:kENPopUpOverlayViewTag];
    [self performDismissAnimationInSourceView:sourceView withBlurView:blurView popupView:popupView overlayView:overlayView completion:completionBlock];
}

#pragma mark - Getters & Setters
- (UIViewController *)en_popupViewController
{
    return objc_getAssociatedObject(self, ENPopupViewControllerPropertyKey);
}

- (void)setEn_popupViewController:(UIViewController *)en_popupViewController
{
    objc_setAssociatedObject(self, ENPopupViewControllerPropertyKey, en_popupViewController, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

}

#pragma mark - View Handling
- (void)presentPopUpView:(UIView *)popUpView completion:(void (^)(void))completionBlock
{
    UIView *sourceView = [self topView];
    // Check if source view controller is not in destination
    if ([sourceView.subviews containsObject:popUpView]) return;

    // Add overlay
    UIView *overlayView = [[UIView alloc] initWithFrame:sourceView.bounds];
    overlayView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    overlayView.tag = kENPopUpOverlayViewTag;
    overlayView.backgroundColor = [UIColor clearColor];

    // Add Blured View
    JWBlurView *bluredView = [[JWBlurView alloc] initWithFrame:overlayView.bounds];
    bluredView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    bluredView.tag = kENPopUpBluredViewTag;
    [bluredView setBlurAlpha:.0f];
    [bluredView setAlpha:.0f];
    [bluredView setBlurColor:[UIColor clearColor]];
    bluredView.backgroundColor = [UIColor clearColor];
    [overlayView addSubview:bluredView];

    // Make the background clickable
    UIButton * dismissButton = [UIButton buttonWithType:UIButtonTypeCustom];
    dismissButton.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    dismissButton.backgroundColor = [UIColor clearColor];
    dismissButton.frame = sourceView.bounds;
    [overlayView addSubview:dismissButton];

    [dismissButton addTarget:self action:@selector(dismissPopUpViewController)
            forControlEvents:UIControlEventTouchUpInside];

    CGFloat buttonSize = 40;

    UIButton * disButton = [[UIButton alloc] init];
    disButton.backgroundColor = [UIColor redColor];

//        UIImage *btnImage = [UIImage imageNamed:@"23"];
//        [disButton setImage:btnImage forState:UIControlStateNormal];
//    

//        disButton.frame = CGRectMake((self.view.frame.size.width/2) - (buttonSize/2),(self.view.frame.size.height/2) + (self.view.frame.size.height/4) , buttonSize, buttonSize);

  disButton.frame = CGRectMake(150, 523, 40, 40);
     //disButton.frame = CGRectMake(150, 580, 40, 40);

    //disButton.frame = CGRectMake(150, UIScreen.mainScreen().bounds.height - 200, 40, 40);

    [overlayView addSubview:disButton];

    [disButton addTarget:self action:@selector(dismissPopUpViewController)
        forControlEvents:UIControlEventTouchUpInside];
    //
    // Customize popUpView
    popUpView.layer.cornerRadius = 8.0f;
    popUpView.layer.masksToBounds = YES;
    popUpView.layer.zPosition = 99;
    popUpView.tag = kENPopUpViewTag;
    popUpView.center = overlayView.center;
    [popUpView setNeedsLayout];
    [popUpView setNeedsDisplay];

    [overlayView addSubview:popUpView];
    [sourceView addSubview:overlayView];

    [self setAnimationStateFrom:popUpView];
    [self performAppearAnimationWithBlurView:bluredView popupView:popUpView completion:completionBlock];
}



#pragma mark - Animation
- (void)setAnimationStateFrom:(UIView *)view
{
    CALayer *layer = view.layer;
    layer.transform = [self transform3d];
}

- (CATransform3D)transform3d
{
    CATransform3D transform = CATransform3DIdentity;
    transform = CATransform3DTranslate(transform, 0, 200.f, 0);
    transform.m34 = 1.0/800.0;
    transform = CATransform3DRotate(transform, kRotationAngle*M_PI/180.f, 1.f, .0f, .0f);
    CATransform3D scale = CATransform3DMakeScale(.7f, .7f, .7f);
    return CATransform3DConcat(transform, scale);
}

- (void)performAppearAnimationWithBlurView:(JWBlurView *)blurView popupView:(UIView *)popupView completion:(void (^)(void))completionBlock
{

    CATransform3D transform;
    transform = CATransform3DIdentity;
    [UIView animateWithDuration:kAnimationDuration
                     animations:^ {
                         [self.en_popupViewController viewWillAppear:NO];
                         [blurView setAlpha:1.f];
                         popupView.layer.transform = transform;
                     }
                     completion:^(BOOL finished) {
                         [self.en_popupViewController viewDidAppear:NO];
                         if (completionBlock != nil) {
                             completionBlock();
                         }
                     }];
}


- (void)performDismissAnimationInSourceView:(UIView *)sourceView
                               withBlurView:(JWBlurView *)blurView
                                  popupView:(UIView *)popupView
                                overlayView:(UIView *)overlayView
                                 completion:(void (^)(void))completionBlock
{
    CATransform3D transform = [self transform3d];
    [UIView animateWithDuration:kAnimationDuration
                     animations:^ {
                         [self.en_popupViewController viewWillDisappear:NO];
                         [blurView setAlpha:0.f];
                         popupView.layer.transform = transform;
                     }
                     completion:^(BOOL finished) {
                         [popupView removeFromSuperview];
                         [blurView  removeFromSuperview];
                         [overlayView  removeFromSuperview];
                         [self.en_popupViewController viewDidDisappear:NO];
                         self.en_popupViewController = nil;
                         if (completionBlock != nil) {
                             completionBlock();
                         }
                     }];
}

#pragma mark - Getters

- (UIView*)topView {
    UIViewController *recentView = self;
    while (recentView.parentViewController != nil) {
        recentView = recentView.parentViewController;
    }
    return recentView.view;
}

@end

My problem is here:

    CGFloat buttonSize = 40;

    UIButton * disButton = [[UIButton alloc] init];
    disButton.backgroundColor = [UIColor redColor];
    disButton.frame = CGRectMake(150, 523, 40, 40);

   [overlayView addSubview:disButton];

    [disButton addTarget:self action:@selector(dismissPopUpViewController)
        forControlEvents:UIControlEventTouchUpInside];

My problem is when i set button at botton.my position is differ for un all simulator (4s,5,5s simulator).Kindly help me out

Edited

I use one seperate viewconroller and i have put the story board identofier name and i used that in my main viewcontroller.

- (IBAction)btnSelectDatePressed:(id)sender
{

    UIViewController *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"PopUp"];
    //vc.view.frame = CGRectMake(0, 0, 309.0f, 531.0f);
    vc.view.frame = CGRectMake(50, 72, 270.0f, 380.0f);
   // vc.view.frame = CGRectMake(0, 0, 270.0f, 380.0f);
    [self presentPopUpViewController:vc];

}

So i cant use story baord for placing my uibuttton at botton.I also don't know there is a solution for that !

Community
  • 1
  • 1
2131
  • 93
  • 1
  • 10
  • 1
    use autolayout, dont even try to place buttons via setting frames, it will be a pain in the ass, the button will appear to big on iPhone 4 and far to big on 6S for example. – luk2302 Sep 27 '15 at 09:39
  • @ luk2302 yes ,but i use button in one viewcontroller for pop up..i cant set uibutton via story board.see my updated post under edit – 2131 Sep 27 '15 at 09:44
  • Who said anything about storyboards? Just use auto layout. If you don't know what that means then google a tutorial or something. – Fogmeister Sep 27 '15 at 09:48
  • you means to use auto layout via code right? – 2131 Sep 27 '15 at 09:55
  • 1
    I don't understand why you think you can't use a storyboard but as @Fogmeister says you can use auto layout without storyboards - it is just harder. The main reason your absolute positioning isn't working how you want is that the origin (0,0) is at the top left of the screen, but different devices have different widths and heights, so your button is always appearing at the same point relative to top,left but at a different point relative to bottom,right. It is for this very reason that auto layout was invented – Paulw11 Sep 27 '15 at 10:08
  • 1
    Please don't repeat your question, edit the original question to add more details if you aren't getting answers. – Abizern Sep 27 '15 at 11:23

0 Answers0