我已经为iOS开发了很长一段时间了,但我总是遇到自定义提醒/弹出窗口的问题,我只是不知道添加自定义视图的正确方式是什么作为应用程序中所有其他视图的弹出窗口。
我已经在网上查了一下,有几种方法可以做到,但创建这些自定义提醒/弹出窗口的最佳方法是什么?
有一个CustomViewController
类,这就是我创建它并将其添加为弹出窗口的方法:
创建新的UIViewController
在UIView
的{{1}}文件中添加UIViewController
作为XIB
的子项(这将保留弹出元素)
在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
中添加了- (void)dealloc
方法,以了解CustomViewController
是否正确发布,但由于某种原因,UIViewController
方法在dealloc
之后被调用{1}}而不是presentViewController
之后。
我想要实现的另一件事:dismissShareViewController
没有CustomViewController
,当我尝试展示UIStatusBar
时,我总是让MFMailComposeViewController
显示没有MFMailComposeViewController
{1}}或白色状态栏,但我无法再将其更改为默认值。我在UIStatusBar
上尝试了几个问题,但没有运气,它就不会改变。
Putting a UIView or UIWindow above Statusbar
请一劳永逸地帮助我。
答案 0 :(得分:7)
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];
}