2

I am upgrading my app to use iOS 13. I have been using a UIWindow to add image to the current screen. However in ios 13 the image will appear, but then disappear straight away. I tried building in previous version, which is ios 12.2, and it's working fine.

this is my code:

 UIWindow *currentWindow = [UIApplication sharedApplication].keyWindow;

    UIImage *image = [UIImage imageNamed:@"test_badge" inBundle:VTBundle compatibleWithTraitCollection:nil];
    UIImageView *badgeImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, [currentWindow bounds].size.height-115, 115, 115)];
    badgeImageView.tag =100101;
    badgeImageView.image = image;

    [currentWindow addSubview:badgeImageView];
Muhammad Fauzi Masykur
  • 1,994
  • 1
  • 15
  • 18
  • you should not add any subview to a `UIWindow` instance directly, that was never a valid configuration of the view-hiearchy on iOS; you should add a [`rootViewController`](https://developer.apple.com/documentation/uikit/uiwindow/1621581-rootviewcontroller) only, then that could have subviews of its own view instance. – holex Oct 01 '19 at 08:58
  • @holex There is essentially nothing wrong with adding views to `UIWindow`. Many applications have legitimate use cases for that. However, in most cases, using a `UIViewController` is a better idea. – Sulthan Oct 01 '19 at 09:16
  • 1
    @Sulthan, fundamentally __everything__ is wrong with adding a subview to a `UIWindow` instance directly, there is no legitimate case to do it; however because it was allowed to do before, devs did that shamelessly – but that does not make it legal. – holex Oct 01 '19 at 09:33
  • @Sulthan agree with on this one , It was allowed before but it was a common crime among all developers to do so , now that apple are evolving i think everyone should take heed in order to adapt. Even though some of these may not have direct effects once the hierachy has changed some effects are pretty visibile . Check out `UISwipeActionStandardButton` . Previously it was under UIView now that it has changed to `UITableView` it is showing significant changes and yet developers are still using it and this is not going to be good in the long run – Anjula Serasinghe Oct 01 '19 at 11:00
  • @holex when you need a view appear on top of the keyboard, you need to get the top window and add the view on it. While it is not a great solution, I don't know any other – Efesus Jan 07 '20 at 19:12
  • @Efesus, the proper solution is to create a new instance of `UIWindow` with custom `windowLevel` value (e.g. `alert`), and adding a proper `rootViewController` to it – instead of messing with the current key-window's stack manually. – holex Jan 08 '20 at 13:17

0 Answers0