How to make a UIView always appear at the front?

2019-01-16 11:14发布

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;

标签: iphone uiview
4条回答
何必那么认真
2楼-- · 2019-01-16 11:34

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楼-- · 2019-01-16 11:44

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

查看更多
混吃等死
4楼-- · 2019-01-16 11:48

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:.

查看更多
Ridiculous、
5楼-- · 2019-01-16 11:56

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;
}
查看更多
登录 后发表回答