创建自定义弹出窗口/警报 UIView/UIViewController 的正确方法
The right way for creating a custom popup / alert UIView / UIViewController
我已经为 iOS 开发了一段时间,但我总是遇到自定义警报/弹出窗口的问题,我只是不知道什么是 正确的 将自定义视图作为弹出窗口添加到应用程序中所有其他视图之上的方法。
我在网上查过,有几种方法可以做到这一点,但是创建这些自定义警报/弹出窗口的最佳方法是什么?
有一个 CustomViewController
class,这就是我创建它并将其添加为弹出窗口的方式:
新建一个UIViewController
在 UIViewController
的 XIB
文件中添加一个 UIView
作为 self.view
的子文件(这将在其中保存弹出元素)
在 UIViewController
中添加此方法以呈现视图:
- (void)presentViewController
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.windowLevel = UIWindowLevelStatusBar;
self.window.screen = [UIScreen mainScreen];
[self.window makeKeyAndVisible];
UIWindow *mainWindow = [[[UIApplication sharedApplication] delegate] window];
[mainWindow setUserInteractionEnabled:NO];
[self.window addSubview:self.view];
self.view.alpha = 0.0f;
self.backgroundImage.image = [self takeSnapshot];
[UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
self.view.transform = CGAffineTransformIdentity;
self.view.alpha = 1.0f;
} completion:nil];
}
在 UIViewController
中添加此方法以删除视图:
- (void)dismissShareViewController
{
[UIView animateWithDuration:DEFAULT_ANIMATION_DURATION
animations:^{
self.view.alpha = 0.0f;
} completion:^(BOOL finished) {
[self.view removeFromSuperview];
self.window = nil;
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];
UIWindow *mainWindow = [[[UIApplication sharedApplication] delegate] window];
[mainWindow setUserInteractionEnabled:YES];
}];
}
用法:
正在为 UIViewController
创建 属性
@property (strong, nonatomic) CustomViewController *viewController;
初始化并呈现
- (void)showCustomViewController
{
self.viewController = [[CustomViewController alloc] init];
[self.viewController presentViewController];
}
问题是我总是得到 Received Memory Warning
。此外,我在 CustomViewController
中添加了一个 - (void)dealloc
方法来了解 UIViewController
是否正确释放,但由于某种原因 dealloc
方法在 presentViewController
而不是在 dismissShareViewController
之后。
我想要实现的另一件事:这个 CustomViewController
没有 UIStatusBar
并且当我尝试显示 MFMailComposeViewController
时,我总是让 MFMailComposeViewController
显示没有a UIStatusBar
或带有白色状态栏,但我无法再次将其更改为默认值。我已经在 Whosebug
上尝试了几个问题,但没有任何运气,它就是不会改变。
Putting a UIView or UIWindow above Statusbar
拜托,一劳永逸,有人会帮助我。
UIAlertView 是 UIView 的子类。如果你想创建一个自定义警报视图,我也会将 UIView 子类化,这样就可以很容易地用你的新自定义视图替换你现有的 UIAlertView。
您可以将自定义警报视图放在最顶层的 UIWindow 上以覆盖所有内容。
@interface MyAlertView : UIView
- (void)duplicateAllTheFunctionsOfUIAlertViewHere;
@end
@implementation MyAlertView : UIView
- (id)initWithFrame:(CGRectMake)frame {
// add your custom subviews here
// An example of a custom background color:
self.backgroundColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:0.5];
}
- (void)duplicateAllTheFunctionsOfUIAlertViewHere {
//re-implement all the functionality in your custom ways
}
- (void)show
{
UIView* superview = [UIApplication sharedApplication].keyWindow;
self.frame = superview.bounds;
[superview addSubview:self];
// Do an animation if you want to get fancy
}
@end
视图控制器中的用法:
- (void)showAlertView
{
UIView* alertView = [[MyAlertView alloc] initWithTextAndButtons:@"blah"];
[alertView show];
}
我已经为 iOS 开发了一段时间,但我总是遇到自定义警报/弹出窗口的问题,我只是不知道什么是 正确的 将自定义视图作为弹出窗口添加到应用程序中所有其他视图之上的方法。
我在网上查过,有几种方法可以做到这一点,但是创建这些自定义警报/弹出窗口的最佳方法是什么?
有一个 CustomViewController
class,这就是我创建它并将其添加为弹出窗口的方式:
新建一个
UIViewController
在
UIViewController
的XIB
文件中添加一个UIView
作为self.view
的子文件(这将在其中保存弹出元素)在
UIViewController
中添加此方法以呈现视图:- (void)presentViewController { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.window.windowLevel = UIWindowLevelStatusBar; self.window.screen = [UIScreen mainScreen]; [self.window makeKeyAndVisible]; UIWindow *mainWindow = [[[UIApplication sharedApplication] delegate] window]; [mainWindow setUserInteractionEnabled:NO]; [self.window addSubview:self.view]; self.view.alpha = 0.0f; self.backgroundImage.image = [self takeSnapshot]; [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{ self.view.transform = CGAffineTransformIdentity; self.view.alpha = 1.0f; } completion:nil]; }
在
UIViewController
中添加此方法以删除视图:- (void)dismissShareViewController { [UIView animateWithDuration:DEFAULT_ANIMATION_DURATION animations:^{ self.view.alpha = 0.0f; } completion:^(BOOL finished) { [self.view removeFromSuperview]; self.window = nil; [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide]; UIWindow *mainWindow = [[[UIApplication sharedApplication] delegate] window]; [mainWindow setUserInteractionEnabled:YES]; }]; }
用法:
正在为
创建 属性UIViewController
@property (strong, nonatomic) CustomViewController *viewController;
初始化并呈现
- (void)showCustomViewController { self.viewController = [[CustomViewController alloc] init]; [self.viewController presentViewController]; }
问题是我总是得到 Received Memory Warning
。此外,我在 CustomViewController
中添加了一个 - (void)dealloc
方法来了解 UIViewController
是否正确释放,但由于某种原因 dealloc
方法在 presentViewController
而不是在 dismissShareViewController
之后。
我想要实现的另一件事:这个 CustomViewController
没有 UIStatusBar
并且当我尝试显示 MFMailComposeViewController
时,我总是让 MFMailComposeViewController
显示没有a UIStatusBar
或带有白色状态栏,但我无法再次将其更改为默认值。我已经在 Whosebug
上尝试了几个问题,但没有任何运气,它就是不会改变。
Putting a UIView or UIWindow above Statusbar
拜托,一劳永逸,有人会帮助我。
UIAlertView 是 UIView 的子类。如果你想创建一个自定义警报视图,我也会将 UIView 子类化,这样就可以很容易地用你的新自定义视图替换你现有的 UIAlertView。
您可以将自定义警报视图放在最顶层的 UIWindow 上以覆盖所有内容。
@interface MyAlertView : UIView
- (void)duplicateAllTheFunctionsOfUIAlertViewHere;
@end
@implementation MyAlertView : UIView
- (id)initWithFrame:(CGRectMake)frame {
// add your custom subviews here
// An example of a custom background color:
self.backgroundColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:0.5];
}
- (void)duplicateAllTheFunctionsOfUIAlertViewHere {
//re-implement all the functionality in your custom ways
}
- (void)show
{
UIView* superview = [UIApplication sharedApplication].keyWindow;
self.frame = superview.bounds;
[superview addSubview:self];
// Do an animation if you want to get fancy
}
@end
视图控制器中的用法:
- (void)showAlertView
{
UIView* alertView = [[MyAlertView alloc] initWithTextAndButtons:@"blah"];
[alertView show];
}