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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…