Mayank Patel Mayank Patel - 1 year ago 108
iOS Question

UIView as popup landscape autolayout issue

I have created a simple popup but issue is when i m trying to landscape that popup then it not working properly

NOTE : i am using Autolayout

Here's my code

mpop = [[MPopUp alloc] init];
mpop.frame = CGRectMake(((self.view.frame.size.width) -
mpop.frame.size.width) / 2,
((self.view.frame.size.height) -
mpop.frame.size.height) / 2,

UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(0,

mpop.overlay = newView;

UIVisualEffect *blurEffect;
blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];

UIVisualEffectView *visualEffectView;
visualEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
visualEffectView.backgroundColor = [UIColor blackColor];
visualEffectView.alpha = 0.4;
visualEffectView.frame = newView.bounds;
[newView addSubview:visualEffectView];

newView.translatesAutoresizingMaskIntoConstraints = NO;

[self.view addSubview:newView];
[mpop showInView:self.view animated:YES];

It working fine in Portrait mode but not working in landscape can anyone help me out?

Answer Source

You need to add auto layout constraints for this situation. I have added code to your sample and posed on GitHub for your reference. find it in the below link. I had to add code in 2 files, in the view controller.m file and view.m file.

Check by running it. If you want to make the width or height dynamic you can replace hard coded value with a calculation like below.

[[UIScreen mainScreen] bounds].size.width * (290.0/320.0) instead of 290.0

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download