The problem

One of the problems you might encounter in development is that a View on the screen is not covered by any View? If you encounter a similar problem, this article and examples may be enlightening.

rendering





superview.gif

implementation

UIWindow understand

To do this, you first need to understand UIWindow and how an application displays to the screen through UIWindow. Here’s an article that goes into great detail. There are two things about UIWindow:

  • UIWindow is a special UIView, and an app has to have at least one UIWindow, whether it’s UIView or UIViewController, and eventually you have to use UIWindow to draw the view to the screen in order to display it.

  • UIWindow has Windows levels, and the higher the level, the top of the screen. At the same level, the UIWindow that follows is displayed on top. Three common levels are provided:

    • UIWindowLevelNormal —> 0
    • UIWindowLevelAlert —> 2000
    • UIWindowLevelStatusBar –> 1000 The UIRemoteKeyboardWindow level where the keyboard is located is 10000000.

Implementation principle of SuperView

  • Create a separate UIWindow with the same size as SuperView. Calling the windowmakeKeyAndVisibleMethods. Don’t forget to adjust the superView frame in the window.
- (void)show {

    UIWindow *currentKeyWindow = [UIApplication sharedApplication].keyWindow;

    NSLog(@"super view frame: %@".NSStringFromCGRect(self.frame));
    if(! _superviewWindow) { _superviewWindow = [[BQSuperViewWindow alloc] initWithFrame:_currentFrame]; _superviewWindow.rootViewController = [ BQSuperViewController new]; }else {
        _superviewWindow.frame = _currentFrame;
    }

    [_superviewWindow makeKeyAndVisible];

    self.frame = CGRectMake(0.0.self.frame.size.width, self.frame.size.height);

    self.layer.cornerRadius = self.frame.size.width <= self.frame.size.height ? self.frame.size.width / 2.0 : self.frame.size.height / 2.0;

    [_superviewWindow addSubview:self];

    // keep the original keyWindow to avoid some unpredictable problems
    [currentKeyWindow makeKeyWindow];

}Copy the code

To display at the top of the screen, the window must have a large windowLevel.

  • Add a drag gesture to update the window frame based on the position of the drag. As for the dynamic boundary control is actually quite simple, there is a link behind the detailed code.
[UIView animateWithDuration:.25 animations:^{
            _superviewWindow.center = newCenter;
            //
            if (_superviewWindow.hidden == YES) {
                self.center = newCenter;
            }

            // record frame for superview back to superviewWindow
            _currentFrame = _superviewWindow.frame;
        }];Copy the code
  • Add keyboard pop-up and hide notifications, hide separate Windows when the keyboard is displayed. Then convert the superView frame in the screen to UIRemoteKeyboardWindow. When the keyboard is hidden, the standalone window is redisplayed, and the position of the window is set and displayed according to the latest frame in the superView(frame may change).
- (void)keyboardIsShown:(NSNotification *)note {

    [self convertSuperViewToKeyboardWindow];
}
- (void)keyboardDidHide {

    [self show];
}

- (void)convertSuperViewToKeyboardWindow {
    //hide the superViewWindow
    [self hide];

    // add superView in keyboardWindow
    self.frame = _currentFrame;

    [[self keyboardWindow] addSubview:self];
}Copy the code

The last

1. The complete implementation code is here! 2. If you have a better implementation, you can leave a comment in the comments section. Or put it up on Github and I’ll follow up. 3. Don’t forget to like it! Don’t forget to like it! Don’t forget to like it!