Keep a UIview or UIviewcontroller on Top of All Others

Keep a UIView or UIViewController on top of all others

Here are a few approaches you could take for this:

  1. If you're targeting iOS 5+ and iPad only, you can make a top-level view controller which has two contained view controllers. The first would be a view controller for your "TouchDisplay" view. The second would be the application's normal root view controller. (i.e. your existing main view controller; you'll need to set definesPresentationContext to YES on this view controller) Since you're writing the container view controller, you can order those two subviews however you like. There is a WWDC 2011 Talk on view controller containment that goes into great detail about this. This is the most "correct" approach IMHO, because it gives you a view controller for your TouchDisplay view, handles rotation and generally plays nice with others. (This only works on iPad, because on iPhone a new modal view always covers the full screen.)

  2. A more straight-forward approach is to simply add your TouchView to your existing top-level UIWindow as a subview with addSubview:. Most applications don't actually remove the top-level view controller or add new top-level ones; they just present other view controllers from it. A view you add in the top-level window will stay above those. Of course, your app may not follow this rule, in which case you might try option #3 instead. This has rotation gotchas (your view will not auto-rotate when the device rotates, so you need to do this yourself.) You could also force your view back to top, say, on a 1-second timer, if you are having issues with other things covering it. This is also not as nice as option #1 because you don't get a UIViewController, just a UIView.

  3. The most extreme approach is that you can create another UIWindow and give it a higher window level, such as UIWindowLevelAlert and put your TouchDisplay view in that. You can then make the window background transparent, and it will stay above your normal app content. There are lots of gotchas here, especially about auto-rotation and which window is the keyWindow (which is why you should use #1 or #2 instead if you can).

How to make a UIView always appear at the front?

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

how to keep a uiview above other views

Use any of the following UIView methods to control depth.

– bringSubviewToFront:
– sendSubviewToBack:
– insertSubview:atIndex:
– insertSubview:aboveSubview:
– insertSubview:belowSubview:

This is taken from the View Programming Guide docs in the View Hierarchies and Subview Management section,

Each superview stores its subviews in an ordered array and the order in that array also affects the visibility of each subview. If two sibling subviews overlap each other, the one that was added last (or was moved to the end of the subview array) appears on top of the other.

Place UIView on top of all other views

If I read your question right your problem is that you can't actually tap on the button in your new view that's on top of the tableview?

And when you tap it, the UITableView below responds?

This means that the UIButton does not recognize it is tapped, and the event goes to the next subview below it to be processed.

Now, there are many possible causes for such behavior - you may have user interaction disabled for it or the UIView, it may it's Enabled property set to NO or it may be beyond the bonds of the UIView (iOS automatically assumes that if a tap is outside of the bounds rectangle it missed the receiver).

Also, taps are ignored if your alpha is set to 0.0.

You should check all of the above.

How to keep a UIView always showing over multiple views

Subclass UIWindow, and override addSubview() to make sure your overlay view is always on top:

weak var overlay: MyOverlayView!

override func addSubview(_ view: UIView) {
if let overlay = view as? MyOverlayView {
self.overlay = overlay
super.addSubview(overlay)
} else {
self.insertSubview(view, belowSubview: overlay)
}
}

You must make sure the app delegate uses your custom class as the app's main window, and not the superclass UIWindow. For this, remove the "Main storyboard file base name" entry from your Info.plist and instead instantiate the main window manually in your AppDelegate:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: MyCustomWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
self.window = MyCustomWindow(frame: UIScreen.main.bounds)

// 1. Add your overlay view to the window
// (...)

// 2. Load your initial view controller from storyboard, or
// instantiate it programmatically
// (...)

window?.makeKeyAndVisible()
return true
}

Note: I haven't tested this code. Let me know in the comments if there's a problem.

How to show a uiview alway on top?

Since you only every have one window per app, and view's don't have levels, you have to make sure that view stays on top of the hierarchy, no matter what. One relatively easy way is to add it directly to the window above the rest of the interface (the navigation controller):

In applicationDidLaunch:

// After the main navigation controller or tab controller has been added
// either programmatically or in the xib:
UIImage *logo = [UIImage imageNamed:@"logo.png"];
UIImageView *logoView = [[UIImageView alloc] initWithImage:logo];
[self.window addSubview:logoView];

UIView always on top

Have you tried adding the view as a sub view of the navigation view

[self.navigationController.view addSubView:yourView];

As long as you don't add other subviews to your navigation controller view it should stay at the top of the hierarchy. You may have to move its position slightly too.

Subview on top of window view in Swift

It's really simple.

You just add another view to window! And it will be there, on top of the first view you added. For example, this code adds a black view and a white view:

let window = UIApplication.sharedApplication().keyWindow!
let v = UIView(frame: window.bounds)
window.addSubview(v)
v.backgroundColor = UIColor.blackColor()
let v2 = UIView(frame: CGRect(x: 50, y: 50, width: 100, height: 50))
v2.backgroundColor = UIColor.whiteColor()
window.addSubview(v2)

You can also add the new view as a sub view of the first view you added:

let window = UIApplication.sharedApplication().keyWindow!
let v = UIView(frame: window.bounds)
window.addSubview(v)
v.backgroundColor = UIColor.blackColor()
let v2 = UIView(frame: CGRect(x: 50, y: 50, width: 100, height: 50))
v2.backgroundColor = UIColor.whiteColor()
v.addSubview(v2)

Swift 4

let window = UIApplication.shared.keyWindow!
let v = UIView(frame: window.bounds)
window.addSubview(v)
v.backgroundColor = .black
let v2 = UIView(frame: CGRect(x: 50, y: 50, width: 100, height: 50))
v2.backgroundColor = UIColor.white
v.addSubview(v2)

Simple!



Related Topics



Leave a reply



Submit