How to make a UIView always appear at the front?

2019-01-16 11:11发布

问题:

Currently I have a UIView which contains some controls. I then have some images I programatically add to the view to display as animations. Currently at the end of each interval of my game loop im having to tell the controller to move the UIView control to the front, otherwise the images will appear on top of it. Is there a less costly method of making it persist as always on top.

Currently I have the following at the end of my game loop:

[self.view bringSubviewToFront:myControlView];

Could I do something like this when the game initiates:

myControlView.alwaysOnTop = true;

回答1:

Rather than using -addSubview: to insert your images, use -insertSubview:belowSubview: and pass your UIView as the second parameter:

[self.view insertSubview:myImage belowSubview:myControlView];

Note that for similar purposes you also have access to the methods -insertSubview:aboveSubview: and -insertSubview:atIndex:.



回答2:

Objective-C

#import <QuartzCore/QuartzCore.h>
myAlwaysOnTopView.layer.zPosition = MAXFLOAT;

Swift 2

myAlwaysOnTopView.layer.zPosition = .max

Swift 3

myAlwaysOnTopView.layer.zPosition = .greatestFiniteMagnitude

This solution is better, especially if you want your view to be always on top, regardless of other views added after it. Just add any other view using addSubview: and it will always remains on top.



回答3:

The other option is to place your view on another UIWindow with higher windowLevel. Learn from Flipboard FLEX or my simplified Paramount

Manager.action = {
  print("action touched")
}

Manager.show()

Take a look at the FLEXWindow on how to choose proper windowLevel and handle touch

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        // Some apps have windows at UIWindowLevelStatusBar + n.
        // If we make the window level too high, we block out UIAlertViews.
        // There's a balance between staying above the app's windows and staying below alerts.
        // UIWindowLevelStatusBar + 100 seems to hit that balance.
        self.windowLevel = UIWindowLevelStatusBar + 100.0;
    }
    return self;
}

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    BOOL pointInside = NO;
    if ([self.eventDelegate shouldHandleTouchAtPoint:point]) {
        pointInside = [super pointInside:point withEvent:event];
    }
    return pointInside;
}


回答4:

to make a subview move to the back of all other current subviews, use view.sendSubviewToBack(yourSubview).



标签: iphone uiview