How to make a UIView always appear at the front?

IphoneUiview

Iphone Problem Overview


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 Solutions


Solution 1 - Iphone

Objective-C

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

Swift 2

myAlwaysOnTopView.layer.zPosition = .max

Swift 3

myAlwaysOnTopView.layer.zPosition = .greatestFiniteMagnitude

Swift 5.3

view.layer.zPosition = CGFloat(Float.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.

IMPORTANT: this solution will make the UIView appear on top, but it will still be below other views for the UI system. That is, any user interaction with the view will generally not work, because it is handled by other overlapping views first.

Solution 2 - Iphone

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

Solution 3 - Iphone

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;
}

Solution 4 - Iphone

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

Solution 5 - Iphone

Another solution would be to create an invisible container view for all your other views. Add that container view first to your main view, and the UIView you want to have on top after that. Then add all your other views to the container view, instead of to the main view.

Solution 6 - Iphone

just remember there is this method too,

- (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionwoot586View Question on Stackoverflow
Solution 1 - IphoneMuhammad Hassan NasrView Answer on Stackoverflow
Solution 2 - IphoneSeamus CampbellView Answer on Stackoverflow
Solution 3 - Iphoneonmyway133View Answer on Stackoverflow
Solution 4 - IphonePeiweiChenView Answer on Stackoverflow
Solution 5 - IphonefishinearView Answer on Stackoverflow
Solution 6 - IphoneFrostmourneView Answer on Stackoverflow