UIView没有出现在UIViewController上(UIView Not Appearing On UIViewController)

编程入门 行业动态 更新时间:2024-10-26 05:29:40
UIView没有出现在UIViewController上(UIView Not Appearing On UIViewController)

我有代码在currentViewController的状态栏上显示UIView。 这个代码在第一个ViewController上运行得很完美,但是当这个代码从另一个viewController运行时,状态栏会消失,但标签从不动画也不会显示出来?

第二个VC嵌入在NavigationController中。 这是唯一的区别。

我无法弄清楚为什么这不起作用,有没有人碰到这个?

+ (UIView *) initializeSuccessfulSparkViewOnView: (UIView *) view { // Get View Width CGRect viewRect = view.window.frame; CGFloat viewWidth = viewRect.size.width; // Custom popup for new users UIView *successfulSparkView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, viewWidth, 20)]; [successfulSparkView setCenter: CGPointMake(viewWidth/2, -10)]; [successfulSparkView setBackgroundColor: [UIColor colorWithHexString: @"FF4300"]]; // Border [[successfulSparkView layer] setBorderWidth: .30f]; [[successfulSparkView layer] setBorderColor: [UIColor whiteColor].CGColor]; // Drop shadow [[successfulSparkView layer] setShadowRadius: 1.0]; [[successfulSparkView layer] setShadowOpacity: 1.0]; [[successfulSparkView layer] setShadowOffset: CGSizeMake(.1, .1)]; [[successfulSparkView layer] setShadowColor: [UIColor whiteColor].CGColor]; [view addSubview: successfulSparkView]; // Label For Successful Spark Sent UILabel *sparkSentLabel = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, viewWidth, 20)]; [sparkSentLabel setCenter: CGPointMake([[successfulSparkView window] frame].size.width/2, 10)]; // If Device Model is 4 or 5, Make Smaller Text To Fit Screen if ([[UIDevice model] containsString: @"iPhone 4"] || [[UIDevice model] containsString: @"iPhone 5"]) { [sparkSentLabel setFont: [UIFont fontWithName: @"Helvetica Neue" size: 9]]; } else { [sparkSentLabel setFont: [UIFont fontWithName: @"Helvetica Neue" size: 10]]; } // Setup Label On UIView [sparkSentLabel setTextAlignment: NSTextAlignmentCenter]; [sparkSentLabel setTextColor: [UIColor whiteColor]]; [sparkSentLabel setText: @"Your Spark was sent successfully, and will be delivered at the selected time."]; [successfulSparkView addSubview: sparkSentLabel]; [successfulSparkView bringSubviewToFront: sparkSentLabel]; return successfulSparkView; } + (void) presentSuccessfulSparkSentView { //GET CURRENT VIEWCONTROLLER UIViewController *currentViewController = [UIViewController currentViewController]; UIView *successfulSparkView = [UIViewController initializeSuccessfulSparkViewOnView: [currentViewController view]]; CGRect viewRect = successfulSparkView.window.frame; CGFloat viewWidth = viewRect.size.width; //SHOW SUCCESSFUL VIEWCONTROLLER float displayTime = .50; [UIView animateWithDuration: displayTime delay: 0 options: UIViewAnimationOptionCurveEaseOut animations:^{ [successfulSparkView setCenter: CGPointMake(viewWidth/2, 10)]; [[[currentViewController view] window] setWindowLevel: UIWindowLevelStatusBar]; } completion:^(BOOL finished) { }]; //DISMISS SUCCESSFUL VIEWCONTROLLER dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)((displayTime * 4) * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [UIView animateWithDuration: displayTime delay: 0 options: UIViewAnimationOptionCurveEaseOut animations: ^{ [successfulSparkView setCenter: CGPointMake(viewWidth/2, -10)]; } completion:^(BOOL finished) { [[[currentViewController view] window] setWindowLevel: UIWindowLevelNormal]; }]; }); }

I have code to present a UIView over the status bar of the currentViewController. This code works on the first ViewController flawlessly, but when this code is ran from another viewController, the status bar goes away, but the label never animates nor does it show up?

The second VC is embedded in a NavigationController. That's the only difference.

I can't figure out why this isn't working, has anyone ran into this?

+ (UIView *) initializeSuccessfulSparkViewOnView: (UIView *) view { // Get View Width CGRect viewRect = view.window.frame; CGFloat viewWidth = viewRect.size.width; // Custom popup for new users UIView *successfulSparkView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, viewWidth, 20)]; [successfulSparkView setCenter: CGPointMake(viewWidth/2, -10)]; [successfulSparkView setBackgroundColor: [UIColor colorWithHexString: @"FF4300"]]; // Border [[successfulSparkView layer] setBorderWidth: .30f]; [[successfulSparkView layer] setBorderColor: [UIColor whiteColor].CGColor]; // Drop shadow [[successfulSparkView layer] setShadowRadius: 1.0]; [[successfulSparkView layer] setShadowOpacity: 1.0]; [[successfulSparkView layer] setShadowOffset: CGSizeMake(.1, .1)]; [[successfulSparkView layer] setShadowColor: [UIColor whiteColor].CGColor]; [view addSubview: successfulSparkView]; // Label For Successful Spark Sent UILabel *sparkSentLabel = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, viewWidth, 20)]; [sparkSentLabel setCenter: CGPointMake([[successfulSparkView window] frame].size.width/2, 10)]; // If Device Model is 4 or 5, Make Smaller Text To Fit Screen if ([[UIDevice model] containsString: @"iPhone 4"] || [[UIDevice model] containsString: @"iPhone 5"]) { [sparkSentLabel setFont: [UIFont fontWithName: @"Helvetica Neue" size: 9]]; } else { [sparkSentLabel setFont: [UIFont fontWithName: @"Helvetica Neue" size: 10]]; } // Setup Label On UIView [sparkSentLabel setTextAlignment: NSTextAlignmentCenter]; [sparkSentLabel setTextColor: [UIColor whiteColor]]; [sparkSentLabel setText: @"Your Spark was sent successfully, and will be delivered at the selected time."]; [successfulSparkView addSubview: sparkSentLabel]; [successfulSparkView bringSubviewToFront: sparkSentLabel]; return successfulSparkView; } + (void) presentSuccessfulSparkSentView { //GET CURRENT VIEWCONTROLLER UIViewController *currentViewController = [UIViewController currentViewController]; UIView *successfulSparkView = [UIViewController initializeSuccessfulSparkViewOnView: [currentViewController view]]; CGRect viewRect = successfulSparkView.window.frame; CGFloat viewWidth = viewRect.size.width; //SHOW SUCCESSFUL VIEWCONTROLLER float displayTime = .50; [UIView animateWithDuration: displayTime delay: 0 options: UIViewAnimationOptionCurveEaseOut animations:^{ [successfulSparkView setCenter: CGPointMake(viewWidth/2, 10)]; [[[currentViewController view] window] setWindowLevel: UIWindowLevelStatusBar]; } completion:^(BOOL finished) { }]; //DISMISS SUCCESSFUL VIEWCONTROLLER dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)((displayTime * 4) * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [UIView animateWithDuration: displayTime delay: 0 options: UIViewAnimationOptionCurveEaseOut animations: ^{ [successfulSparkView setCenter: CGPointMake(viewWidth/2, -10)]; } completion:^(BOOL finished) { [[[currentViewController view] window] setWindowLevel: UIWindowLevelNormal]; }]; }); }

最满意答案

像这样更改代码

+ (void) presentSuccessfulSparkSentView { //GET CURRENT VIEWCONTROLLER UIViewController *currentViewController = [UIViewController currentViewController]; if(currentViewController.navigationController != nil) { currentViewController = currentViewController.navigationController; } ......

我认为[UIViewController currentViewController]只获取UIViewController,并尝试在其视图上添加UIView。

但是,UINavigationBar位于UIViewController之上,因此如果在UIViewController的视图中添加UIView,UINavigationBar会隐藏它。

最好的方法是在'UIWindow'上添加UIView。

那么你不需要调用[UIViewController currentViewController],你不需要关心UINavigationViewController和其他东西。

Change the code like this

+ (void) presentSuccessfulSparkSentView { //GET CURRENT VIEWCONTROLLER UIViewController *currentViewController = [UIViewController currentViewController]; if(currentViewController.navigationController != nil) { currentViewController = currentViewController.navigationController; } ......

I think that [UIViewController currentViewController] gets only UIViewController and you try to add UIView on it's view.

But, UINavigationBar is above the UIViewController, so If you add UIView on UIViewController's view, UINavigationBar hides it.

The best approach is add UIView on 'UIWindow'.

then you don't need to call [UIViewController currentViewController] and you don't need to care about UINavigationViewController and other things.

更多推荐

本文发布于:2023-07-22 19:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1222775.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:出现在   UIView   Appearing   UIViewController

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!