我已经为iOS开发了一段时间了,但是我总是遇到自定义警报/弹出窗口的问题,我只是不知道将自定义视图作为弹出窗口添加到Windows中所有其他视图之上的正确方法是什么应用程序。
我已经在线检查过,有几种方法可以做到,但是创建这些自定义警报/弹出窗口的最佳方法是什么?
有一个CustomViewController
类,这就是我创建它并将其添加为弹出窗口的方式:
创建一个新的 UIViewController
UIView
在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
方法是在之后presentViewController
而不是dismissShareViewController
应该在之后立即调用的。
我想要实现的另一件事:CustomViewController
没有UIStatusBar
,当我尝试演示时MFMailComposeViewController
,总是MFMailComposeViewController
显示不带UIStatusBar
或带有白色状态栏的显示,但是我无法将其再次更改为默认值。我在这里尝试了几个问题,StackOverflow
但是没有任何运气,它只是不会改变。
请一劳永逸,有人会帮助我。
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];
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句