Yos*_*far 4 objective-c uiviewcontroller uiview ios
我已经为iOS开发了很长一段时间了,但我总是遇到自定义警报/弹出窗口的问题,我只是不知道在所有其他视图中添加自定义视图作为弹出窗口的正确方法是什么应用程序.
我已经在网上查了一下,有几种方法可以做到,但创建这些自定义提醒/弹出窗口的最佳方法是什么?
有一个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];
}
Run Code Online (Sandbox Code Playgroud)在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];
}];
}
Run Code Online (Sandbox Code Playgroud)用法:
为...创建属性 UIViewController
@property (strong, nonatomic) CustomViewController *viewController;
Run Code Online (Sandbox Code Playgroud)初始化并呈现
- (void)showCustomViewController
{
self.viewController = [[CustomViewController alloc] init];
[self.viewController presentViewController];
}
Run Code Online (Sandbox Code Playgroud)问题是我总是得到一个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
Run Code Online (Sandbox Code Playgroud)
在视图控制器中的用法:
- (void)showAlertView
{
UIView* alertView = [[MyAlertView alloc] initWithTextAndButtons:@"blah"];
[alertView show];
}
Run Code Online (Sandbox Code Playgroud)