Ios 创建自定义弹出/警报UIView/UIViewController的正确方法
我已经为iOS开发了很长一段时间了,但我总是在自定义警报/弹出窗口方面遇到问题,我只是不知道在应用程序中的所有其他视图之上添加自定义视图作为弹出窗口的正确方法是什么 我在网上查过了,有几种方法可以做到这一点,但创建这些自定义警报/弹出窗口的最佳方法是什么 有一个Ios 创建自定义弹出/警报UIView/UIViewController的正确方法,ios,objective-c,uiview,uiviewcontroller,Ios,Objective C,Uiview,Uiviewcontroller,我已经为iOS开发了很长一段时间了,但我总是在自定义警报/弹出窗口方面遇到问题,我只是不知道在应用程序中的所有其他视图之上添加自定义视图作为弹出窗口的正确方法是什么 我在网上查过了,有几种方法可以做到这一点,但创建这些自定义警报/弹出窗口的最佳方法是什么 有一个CustomViewController类,我就是这样创建它并将其添加为弹出窗口的: 创建新的UIViewController 在UIViewController的XIB文件中添加UIView,作为self.view的子文件(这将在其中保
CustomViewController
类,我就是这样创建它并将其添加为弹出窗口的:
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];
}
收到的内存警告
。我还在CustomViewController
中添加了-(void)dealloc
方法,以了解UIViewController
是否正确释放,但是由于某种原因,dealloc
方法在presentViewController
之后被调用,而不是在解除ShareViewController
之后被调用
我想实现的另一件事是:这个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];
}
为什么不使用UIAlertView呢?因为我不想使用
UIAlertView
,我想用alpha覆盖整个屏幕。答案很好!谢谢请注意,不建议这样做。Apple docs不允许这样做:“UIAlertView类旨在按原样使用,不支持子类化。该类的视图层次结构是私有的,不能修改。”@dan我没有将UIAlertView子类化。我把UIView分为两个子类。