How to tell if UIViewController's view is visible
The view's window property is non-nil if a view is currently visible, so check the main view in the view controller:
Invoking the view method causes the view to load (if it is not loaded) which is unnecessary and may be undesirable. It would be better to check first to see if it is already loaded. I've added the call to isViewLoaded to avoid this problem.
if (viewController.isViewLoaded && viewController.view.window) {
// viewController is visible
}
Since iOS9 it has became easier:
if viewController.viewIfLoaded?.window != nil {
// viewController is visible
}
Or if you have a UINavigationController managing the view controllers, you could check its visibleViewController property instead.
How do I check if an UIViewController is currently being displayed?
You need to check if your viewcontroller is on top of the stack of navigationcontroller's viewcontroller array. A sample code is,
if (self.navigationController.topViewController == self) {
//the view is currently displayed
}
You can use this inside the viewWillAppear
method to check whether the current view is visible.
How do i check if another view controller is visible?
As a strict response to your question you could check if the ViewController
's view has his window:
if ViewController.view.window != nil {
}
Anyway I encourage you to take @Wain ' s advice.
How to check if a specific UIViewController's view is currently visible?
After doing some research, I found this answer in a different question posted on here...This seems to be the best way...
The view's window property is non-nil if a view is currently visible, so check the main view in the view controller:
if (viewController.isViewLoaded && viewController.view.window){
// viewController is visible
}
How to check if view, not viewController, is visible or not?
A UIView has a superview
property and a window
property. You can check to see if those are nil
. If the view has a nil superview, then it has not been added to anything and is not visible.
If the view does have a superview property that is not nil
then you can look at the subviews
array property of the superview to determine the stack of views (0 is at the back).
If your view is not at the front of the stack, you would need to look at the frame rects of the other views in front of it to determine if they are covering it. Personally, I would use the CGRectIntersection
command to test.
A UIView also has a hidden
property which you can check as well as an alpha
property which would make the view invisible if it was set to 0.
How do I check from a view controller or class if a specific view controller is visible on screen? Or how do I check visible view controller?
If you don't have that view controller's instance, what you can do is to create a statice variable in the view controller and set that to true when the controller is visible and false when it is invisible.
Here's some sample code. In your ChatViewController
public static var isOnScreen = false
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
isOnScreen = true
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
isOnScreen = false
}
Now you can check this variable any time to know if the controller is visible or not like this.
if ChatViewController.isOnScreen {
}
How can I tell if a UIView is in front (visible)?
UIView's don't necessarily have a concept of being in front. UIWindows can be key or not, but it's not quite the same thing.
You can bring a view to the front, but that doesn't mean it is or is not visible. Remember, views can be any size.
A UIView buried deep in the hierarchy could be partially visible, it could be obscured, or it could be behind some translucent view. Likewise a view at the front may not be visible at all if its opacity value or hidden flags are modified.
I think what you want to do is check the subviews NSArray of your superview or UIWindow and check that. I can't remember which is the front, but it is either the first or last object.
Subviews are drawn with the painter's method. The views are drawn in order from farthest to nearest and the last object drawn is "the front."
Determine if UIView is visible to the user?
You can check if:
- it is hidden, by checking view.hidden
- it is in the view hierarchy, by checking
view.superview != nil
- you can check the bounds of a view to see if it is on screen
The only other thing I can think of is if your view is buried behind others and can't be seen for that reason. You may have to go through all the views that come after to see if they obscure your view.
Related Topics
How to Check How Long Ago Was the App Last Opened
Why It Shows "Ld: Framework Not Found Bolts"
How to Access a Specific Field from Cloud Firestore Firebase in Swift
How to Record Video of Front and Back Camera At a Time in Ios
Scrolling Issue on Position Fixed Element on Ios
Check If There Is an Emoji Contained in a String
How to Get the Root Directory of My iOS App
Wkwebview Not Loading Webpage - Renders Blank Screen in Swift
How to Center Horizontally Uicollectionview Cells
How to Create a Custom Pop Up View With Swift
Iphone/Ipad: How to Make Uitextfield Readonly (But Not Disabled)
Uiimageview - How to Get the File Name of the Image Assigned
How to Load Image from Local Path iOS Swift (By Path)
Take Screenshots in the iOS Simulator
Detect Tap on a Button in Uitableviewcell for Uitableview Containing Multiple Sections
How to Change the Title of the "Back" Button on a Navigation Bar