2

I am designing an alert view in which I display a custom view when user taps a particular button.I am using a scroll view for my lengthy content view. I have written the code for the same as given below.

I want that this alert view to display right in the middle of your display screen irrespective of scroll position or content view position as alert in iOS by default pop ups. So is there any way to do that. I have tried a way but didn't succeed.

And the only solution I found is to scroll to top before displaying and add the subview. And since this is not perfect,so please suggest some measures or suggestions. Thanks.

 @IBAction func forgotpassword(sender: AnyObject)
        {


                    blurEffect = UIBlurEffect(style: .Dark)
                    blurEffectView = UIVisualEffectView(effect: blurEffect)
                    blurEffectView.frame = self.contentview.bounds
                    blurEffectView.autoresizingMask = [.FlexibleWidth,.FlexibleHeight]



                    self.AlertVieww.frame.size = CGSizeMake(blurEffectView.frame.width-40, self.AlertVieww.frame.height)//AlertVieww is the IBOutlet of my custom view in scene dock

                    AlertVieww.center =  CGPointMake(UIScreen.mainScreen().bounds.size.width/2, UIScreen.mainScreen().bounds.size.height/2);



                    AlertVieww.autoresizingMask = [.FlexibleHeight,.FlexibleWidth]

                    blurEffectView.addSubview(self.AlertVieww)




               [self.scrollview.setContentOffset(CGPointZero, animated: false)]//I scroll to the top 

            UIView.transitionWithView(self.view, duration: 1.0, options: UIViewAnimationOptions.TransitionFlipFromBottom, animations:
                {


                self.contentview.addSubview(self.blurEffectView)

                }, completion: { finished in



                    self.scrollview.scrollEnabled = false

            })




        }
//These are two cnacel buttons in which I remove the views
 @IBAction func sendAlert(sender: AnyObject)
    {
        self.blurEffectView.hidden = true
        self.scrollview.scrollEnabled = true

    }

    @IBAction func cancelAlert(sender: AnyObject)
    {
        self.blurEffectView.hidden = true
        self.scrollview.scrollEnabled = true

    }
G.Abhisek
  • 1,034
  • 11
  • 44
  • U can show it with navigation bar if u have one, or maybe `keyWindow` too – Tj3n Jan 05 '16 at 04:55
  • Add your alertView on window. – Muneeba Jan 05 '16 at 05:00
  • @Muneeba adding subview on window makes my autolayout go mad in landscape mode. My alert view gets squeezed compared to my alert view's subviews position. – G.Abhisek Jan 05 '16 at 05:19
  • You can also make a controller for your alertView and present it on window. In that case you won't have those layout issue. – Muneeba Jan 05 '16 at 05:22
  • @Tj3n Ya I do have a navigation bar. So what to do next? – G.Abhisek Jan 05 '16 at 05:25
  • @Muneeba Loading a view controller for alert. Okay I would try that idea but you see scene dock is quite handy. So if there is any suggestions for its improvement. please suggest. – G.Abhisek Jan 05 '16 at 05:30
  • @G.Abhisek u can try `addSubview` with your `self.navigationController.navigationBar` with the same frame as u add in normal `self.view` – Tj3n Jan 05 '16 at 05:58
  • But by doing that my 2 button does not function properly or their methods are not being called on pressing them. – G.Abhisek Jan 05 '16 at 06:07
  • @Muneeba I tried to do adding a separate view controller's View as the blurred view's subview and then added the blurred view to the window but it is not solving my cause. – G.Abhisek Jan 05 '16 at 06:46
  • No! In your viewController's nib.. add your blurView and the alertView. and on window's root controller just present this controller.Like this [self.window.rootViewController presentViewController:controller animated:YES completion:nil]; .Make sure to add proper constraint for the alert view in nib. – Muneeba Jan 05 '16 at 06:47
  • @Muneeba Oh you are suggesting to have xib files for AlertView and Blur View. I can't get you. Please give a descriptive answer so that any readers of this question can be helpful from your answer. – G.Abhisek Jan 05 '16 at 06:54

7 Answers7

1

try with that add your alert view in topmost controller's view instead of blurEffectView and set center of alertview isequal to topmost controller's view's center

like ,

 AlertVieww.center = UIApplication.sharedApplication().keyWindow?.rootViewController?.view.center
 UIApplication.sharedApplication().keyWindow?.rootViewController?.view.addSubview(self.AlertVieww)
Jaydeep Patel
  • 1,699
  • 17
  • 30
  • Sorry this made my both cancel and send button on my Alert view not to work. Please see my edited question for the functionality of the 2 buttons – G.Abhisek Jan 05 '16 at 05:23
  • not working in terms of IBAction is not called or then method inside IBAction functions are not working? – Jaydeep Patel Jan 05 '16 at 05:27
  • So what can be done for that. Because my current code works very fine but the only problem is the centering . – G.Abhisek Jan 05 '16 at 05:32
  • if you want to change only frame in your code then try with this `AlertVieww.center = CGPointMake((UIScreen.mainScreen().bounds.size.width/2) - blurEffectView.frame.origin.x, (UIScreen.mainScreen().bounds.size.height/2) - blurEffectView.frame.origin.y);` – Jaydeep Patel Jan 05 '16 at 05:41
  • Sorry but the view gets added on the top but not on the display screen centre irrespective of the scrolls position. So I once again need to scroll to top in order to get the alert viewed. – G.Abhisek Jan 05 '16 at 05:56
  • try with that `AlertVieww.center = CGPointMake((UIScreen.mainScreen().bounds.size.width/2) + (scrollView.contentOffset.x) - blurEffectView.frame.origin.x, (UIScreen.mainScreen().bounds.size.height/2) + (scrollView.contentOffset.y) - blurEffectView.frame.origin.y);` – Jaydeep Patel Jan 05 '16 at 07:11
0

UIView *subview = your View To Be Centered In Its SuperView;

UIView *superView = subview.superview;

subview.center = [superView convertPoint:superView.center
                                fromView:superView.superview];

If view is nil(on fromView:), this method instead converts from window base coordinates. Otherwise, both view and the receiver must belong to the same UIWindow object.

NOTE: If you use the auto layout stuff, then you have to change the constraints . not the frame or center

Akash
  • 461
  • 2
  • 14
  • I just have a view which I need to display in the centre of my screen. Autolayout shouldn't be a hindrance. – G.Abhisek Jan 05 '16 at 05:36
0

Here is a solution

Instead of self.view.frame.size.width

Use:: [UIScreen mainScreen].bounds.size.width or height

So code is

UIView *View = [[UIView alloc] init]; View.frame = CGRectMake([UIScreen mainScreen].bounds.size.width / 2, [UIScreen mainScreen].bounds.size.height / 2 , 18, 36); [self.view addSubview: View];

Akash
  • 461
  • 2
  • 14
0

here try it this one also to make cGpoint and according your main view:

view.center = CGPointMake(self.view.frame.size.width / 2,

                                         self.view.frame.size.height / 2);
Akash
  • 461
  • 2
  • 14
0

try this one...! self.view.center = CGPointMake(CGRectGetMidX(self.parentView.bounds), CGRectGetMidY(self.parentView.bounds));

Akash
  • 461
  • 2
  • 14
0

remove this code

                AlertVieww.center =  CGPointMake(UIScreen.mainScreen().bounds.size.width/2, UIScreen.mainScreen().bounds.size.height/2);

Add this code

AlertVieww.center = self.blurEffectView.center

after

            self.contentview.addSubview(self.blurEffectView)
Aruna Mudnoor
  • 4,795
  • 14
  • 16
0

One way is to create your own AlertController(inherits from UIViewController) and in its xib, put all the views for your alert along with proper constraint to be correctly shown on iPhone/iPad and whenever you need to show, Just present it on window like the following .

  [window.rootViewController presentViewController:yourAlertControllerInstance animated:YES completion:nil];

OR

You can use this custom alert . It has variety of style along with BlurView in background and you can customise it as per your need.

Muneeba
  • 1,756
  • 10
  • 11
  • It loads the view controller in the entire screen but I want that to be loaded with a particular size with blur effect in background and cancel/dismiss it when the button is pressed. – G.Abhisek Jan 05 '16 at 08:14
  • Check this link for how to presentViewcontroller with transparent background. http://stackoverflow.com/questions/12741224/ios-modal-viewcontroller-with-transparent-background. Or use the third party alert that will make things easy. If that library fulfill your need then don't reinvent the wheel. – Muneeba Jan 05 '16 at 08:56