【问题标题】:The right way for creating a custom popup / alert UIView / UIViewController创建自定义弹出窗口/警报 UIView/UIViewController 的正确方法
【发布时间】:2015-03-22 14:20:25
【问题描述】:

我已经为 iOS 开发了很长一段时间,但是我总是遇到自定义警报/弹出窗口的问题,我只是不知道添加自定义视图的正确方法是什么作为应用中所有其他视图之上的弹出窗口。

我在网上查过,有几种方法可以做到这一点,但创建这些自定义警报/弹出窗口的最佳方法是什么?

有一个 CustomViewController 类,这就是我创建它并将其添加为弹出窗口的方式:

  1. 新建一个UIViewController

  2. UIViewControllerXIB 文件中添加一个UIView 作为self.view 的子文件(这将包含弹出元素)

  3. 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];
    }
    
  4. 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];
                         }];
    }
    

用法:

  1. UIViewController 创建属性

    @property (strong, nonatomic) CustomViewController *viewController;
    
  2. 初始化并呈现

    - (void)showCustomViewController
    {
        self.viewController = [[CustomViewController alloc] init];
        [self.viewController presentViewController];
    }
    

问题是我总是收到Received Memory Warning。此外,我在CustomViewController 中添加了一个- (void)dealloc 方法,以了解UIViewController 是否正确释放,但由于某种原因,dealloc 方法在presentViewController 之后被调用,而不是在@987654341 之后被调用@ 应该的。

我想要实现的另一件事:这个CustomViewController 没有UIStatusBar,当我尝试呈现MFMailComposeViewController 时,我总是让MFMailComposeViewController 显示没有UIStatusBar 或白色状态栏,但我无法再次将其更改为默认值。我在StackOverflow 上尝试了几个问题,但没有任何运气,它不会改变。

Putting a UIView or UIWindow above Statusbar

请永远,有人会帮助我。

【问题讨论】:

  • 为什么不使用 UIAlertView?
  • 因为我不想使用UIAlertView,所以我想要一些东西来覆盖整个屏幕,但是带有alpha。

标签: ios objective-c uiview uiviewcontroller


【解决方案1】:

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];
}

【讨论】:

  • 很好的答案!谢谢!
  • 请注意,不建议这样做。 Apple 文档不允许这样做:“UIAlertView 类旨在按原样使用,不支持子类化。此类的视图层次结构是私有的,不得修改。”developer.apple.com/library/ios/documentation/UIKit/Reference/…
  • @dan 我没有继承 uialertview。我继承了 UIView。
猜你喜欢
  • 2017-11-16
  • 1970-01-01
  • 1970-01-01
  • 2016-08-31
  • 2012-10-31
  • 2021-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多