Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
299 views
in Technique[技术] by (71.8m points)

iphone - How to make a UIView always appear at the front?

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;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...