Reputation: 3965
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;
Upvotes: 54
Views: 42371
Reputation: 6336
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.
Upvotes: 0
Reputation: 2728
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.
Upvotes: 200
Reputation: 174
just remember there is this method too,
- (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview
Upvotes: 0
Reputation: 48065
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;
}
Upvotes: 3
Reputation: 413
to make a subview move to the back of all other current subviews, use view.sendSubviewToBack(yourSubview)
.
Upvotes: 1
Reputation: 17906
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:
.
Upvotes: 26