I'm presenting a NavigationViewController in iPad app as formSheet, which works great. But in this presented NavigationViewController when I push QLPreviewController with one Image to display it resizes it self to fit the Image. This alone would not have been a problem but upon going back to previous viewController form QLPreviewController the resized viewController size affect all ViewControllers in that NavgationViewController.
If there is any way to go back to default size for viewController on coming back from QLPreviewController it would help too. Or If QLPreviewController doesn't resize when pushed.
Also this problem is only in iOS 13+.
Any help would be appreciated.
Finally, I resolved it on my own.
Create one Variable
var defaultPreferredContentSize: CGSize?
and one method
fileprivate func adjustViewControllerSizeIfNeeded() {
if let size = defaultPreferredContentSize {
if self.navigationController?.preferredContentSize != size {
self.navigationController?.preferredContentSize = size
}
} else {
defaultPreferredContentSize = self.view.frame.size
}
}
Call this method in viewDidAppear
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
adjustViewControllerSizeIfNeeded()
}
Related
I have a basic app with a UITabBarController as the root view controller. When a user of the app is not signed in I'm showing a modal controller via window!.rootViewController!.present(viewController, animated: true) in my AppDelegate. This works fine on all iPhone models, however the following happens on any iPad:
The background color of the SignInController is visible during the transition. Now comes the weird thing: When I change the view in Interface Builder to an iPad the bug is gone like so:
Changing the background color back to the transparent default removes at least the white background, however the view is still animating from the left bottom which is something I don't want. And by the way, changing the view in Interface Builder breaks the animation on all iPhones. Changing it back fixes it but breaks again all iPads.
This is the code (using ReSwift for state management):
func newState(state: State) {
switch (previousState.session, state.session) {
case (.loading, .notSignedIn), (.signedIn, .loading):
(window!.rootViewController! as! UITabBarController).selectedIndex = 0
let viewController = storyboard.instantiateViewController(withIdentifier: "SignInViewController")
window!.rootViewController!.present(viewController, animated: true, completion: nil)
default:
// more stuff
break
}
}
EDIT: Added the actual code.
I fixed it! 😊
The problem was a combination of having an observer on keyboardWillShowNotification and a becomeFirstResponder in the viewWillAppear method of the presented controller.
Moving the becomeFirstResponder into viewDidAppear fixed all the problems!
Thanks man! Saved my day.. I'm presenting the keyboard from within a tableview cell - I fixed it like this:
private var canPresentKeyboard: Bool = false
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
canPresentKeyboard = true
if _currentlySelectedIdType != .image {
reload(section: .idType)
}
}
func configure(cell: NumberIdTableViewCellInput) {
cell.set(delegate: self)
if canPresentKeyboard {
cell.clearAndSetFirstResponder()
}
}
I know the code is a bit out of context, but I believe the intention is clear.
Since the problem is hard to explain, here is a video attached.
The behavior above happens when I select a cell on a table view controller and it performs a segue to a tab bar controller. During the transition, I rotate the device to landscape mode and if done fast enough, the tab bar will disappear and the view will load on half of the screen and the other half stays black.
I did this in my app and I thought I was doing something wrong, so I made a new app with the same structure but no view controllers and this happens too.
I noticed, if the perform segue is not animated, this bug won't appear but I want the animation. I believe that delaying the screen rotation until the view has loaded will fix the problem.
Is there a way to lock rotation until the view has loaded?
Yes there is. But before we proceed to that, since I saw your video/issue, usually when needed, a view must be re-layouted. Not sure about the term, but you can adjust its constraints based on the current orientation.
Anyways, for the way to lock the rotation until you want it be unlocked, I've found this utility quite long time ago.
import UIKit
struct AppUtility {
static func lockOrientation(_ orientation: UIInterfaceOrientationMask) {
if let delegate = UIApplication.shared.delegate as? AppDelegate {
delegate.orientationLock = orientation
}
}
/// OPTIONAL Added method to adjust lock and rotate to the desired orientation
static func lockOrientation(_ orientation: UIInterfaceOrientationMask, andRotateTo rotateOrientation:UIInterfaceOrientation) {
self.lockOrientation(orientation)
UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation")
}
}
This has been very helpful to me. Now to use it, it's no brainer.
class ViewController: UIViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Lock immediately the rotation to portrait!
// Use lockOrientationAndRotateTo if it's needed.
AppUtility.lockOrientation(.portrait, andRotateTo: .portrait)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// Then finally, after loading something, enable rotation.
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(5)) {
// Assuming that the loading takes 5 seconds or so.
AppUtility.lockOrientation(.all)
}
}
}
Good luck! I hope this helps!
After some googling I found that messing with the default Xcode launch screen is not the most proper way for make ur splash screen wait for some time and other stuff so I add new view controller (custom class named splash) to my storyboard and then after 2 seconds it's will display my main UINavigationController and it's not working just freeze on the splash screen
Here is my code:
import UIKit
class splash: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
NSThread.sleepForTimeInterval(2.0)
let vc = storyboard?.instantiateViewControllerWithIdentifier("mainmenu") as! UINavigationController
self.presentViewController(vc, animated: true, completion: nil)
}
}
i have solve it using perform selector
class splash: UIViewController {
override func viewDidLoad() {
super . viewDidLoad()
performSelector(#selector(splash.showmainmenu), withObject: nil, afterDelay: 2)
}
func showmainmenu(){
performSegueWithIdentifier("mainmenu", sender: self)
}
What are you trying to do - is an ugly hack. Don't.
You should create your custom Splash View Controller with layout that mimics your default splash screen image, perform custom animations if any, and then push/present next view controller (on the main thread), or whatever you want to do according to your app requirements.
My navigation controller intermittently will freeze on push. It seems to add the new view controller onto the stack, but the animation never takes place. I also have two other containers that hold view controllers on the screen, and I can interact with both of them just fine after the navigation controller freezes. The really interesting thing is if I try to push another view controller onto the navigation controller's stack, I noticed that there is an extra view controller on top of the stack (the view controller that I pushed initially that froze the navigation controller). So if I'm on the home screen (we'll call it VC-Home) and I try to push a new view (VC-1) and it freezes, then I try to push a new view (VC-2), this is what I see in the current stack before the push:
{ [VC-Home, VC-1] }
and after pushViewController is called, it remains the same; VC-2 is not added to the stack.
From what I can tell, the navigation controller starts the animation by making the previous view controller inactive before the animation begins, but then the animation never takes place, leaving the navigation controller in a frozen state.
I'm creating the new view controller from a storyboard by calling
UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("ViewController") so I don't think there's any issues there. I'm also not overriding pushViewController on the navigation bar. Some unique things about my app is that it is very high-res image heavy (using SDWebImage to manage that) and I always have three containers on the screen at once (one navigation controller, one view controller for search, and one interactive gutter/side/slideout menu).
CPU usage is low and memory usage is normal (steadily around 60-70MB on device when freezes occur).
Are there any ideas with what might be causing this or any debugging tips that could help me discover the real problem?
Update
There's no unique code for the UINavigationController since I'm just pushing using pushViewController(). Here's the code that calls it:
func didSelectItem(profile: SimpleProfile) {
let vc = UIStoryboard.profileViewController()
vc.profile = profile
navigationController?.pushViewController(vc, animated: true)
}
The ViewController that I pushed has the following code in viewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
button.roundView()
if let type = profile?.profileType {
//load multiple view controllers into a view pager based on type
let viewControllers = ProfileTypeTabAdapter.produceViewControllersBasedOnType(type)
loadViewPagerViews(viewControllers)
let topInset = headerView.bounds.height + tabScrollView.contentSize.height
if let viewPager = viewPager {
for view in viewPager.views {
if let tempView = view as? PagingChildViewController {
tempView.profile = fullProfile
tempView.parentVCDelegate = self
tempView.topInset = topInset
}
}
}
}
}
func loadViewPagerViews(viewControllers: [UIViewController]) {
viewPager?.views = viewControllers
viewPager?.delegate = self
//loading views into paging scroll view (using PureLayout to create constraints)
let _ = subviews.map { $0.removeFromSuperview() }
var i = 0
for item in views {
addSubview(item.view)
item.view.autoSetDimensionsToSize(CGSize(width: tabWidth, height: tabHeight))
if i == 0 {
item.view.autoPinEdgeToSuperviewEdge(.Leading)
} else if let previousView = views[i-1].view {
item.view.autoPinEdge(.Leading, toEdge: .Trailing, ofView: previousView)
}
if i == views.count {
item.view.autoPinEdgeToSuperviewEdge(.Trailing)
}
i += 1
}
contentSize = CGSize(width: Double(i)*Double(tabWidth), height: Double(tabHeight))
}
Update 2
I finally got it to freeze again. The app was in the background and I brought it back and tried pushing a view controller on the stack when it froze. I noticed an animation was taking place. I have a scrollview at the top of the page that pages through its content every 10 seconds (think of the app stores top banner). On this freeze, I noticed that the banner was mid-animation.
Here's the scrolling function from the my UIScrollView that gets called every 10 seconds:
func moveToNextItem() {
let pageWidth: CGFloat = CGRectGetWidth(frame)
let maxWidth: CGFloat = pageWidth * CGFloat(max(images.count, profileImages.count))
let contentOffset: CGFloat = self.contentOffset.x
let slideToX = contentOffset + pageWidth
//if this is the end of the line, stop the timer
if contentOffset + pageWidth == maxWidth {
timer?.invalidate()
timer = nil
return
}
scrollRectToVisible(CGRectMake(slideToX, 0, pageWidth, CGRectGetHeight(frame)), animated: true)
}
I don't recall ever having a push stop because of an animation/scroll taking place, but I could be wrong.
I've also rechecked the stack and the same situation as described above is still the case where [VC-Home, VC-1] is the stack and VC-2 is not pushed on. I've also gone through VC-1's variables and everything has loaded (data calls and image loads).
Update 3
This is getting stranger by the second. I've overriden pushViewController so I can put a breakpoint in there and do some debugging based on Alessandro Ornano's response. If I push a view controller unsuccessfully, then send my app to the background, put a breakpoint into the pushViewController call, and bring the app back, the breakpoint is immediately hit a number of times. If I then continue past all the hits, the next view controller suddenly becomes visible and the last view controller I tried to push is now on the stack as the last view controller. This means that the one that I see is still disabled, which essentially puts me in the same position as before.
We have faced the same problem couple of weeks back. And for our problem we narrowed it down to left-edge pop gesture recogniser. You can try and check if you can reproduce this problem using below steps
Try using the left edge pop gesture when there are no view controllers below it (i.e on root view controllers, your VC-Home controller)
Try clicking on any UI elements after this.
If you are able to reproduce the freeze, try disabling the interactivePopGestureRecognizer when the view controller stack have only one view controller.
Refer to this question for more details. Below is the code from the link for ease of reference.
- (void)navigationController:(UINavigationController *)navigationController
didShowViewController:(UIViewController *)viewController
animated:(BOOL)animate
{
if ([self respondsToSelector:#selector(interactivePopGestureRecognizer)])
{
if (self.viewControllers.count > 1)
{
self.interactivePopGestureRecognizer.enabled = YES;
}
else
{
self.interactivePopGestureRecognizer.enabled = NO;
}
}
}
Great answer by #Penkey Suresh! Saved my day! Here's a SWIFT 3 version with a small addition that made the difference for me:
func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
if (navigationController.viewControllers.count > 1)
{
self.navigationController?.interactivePopGestureRecognizer?.delegate = self
navigationController.interactivePopGestureRecognizer?.isEnabled = true;
}
else
{
self.navigationController?.interactivePopGestureRecognizer?.delegate = nil
navigationController.interactivePopGestureRecognizer?.isEnabled = false;
}
}
Just don't forget to add UINavigationControllerDelegate and set the navigationController?.delegate = self
Another important part is to assign the interactivePopGestureRecognizer to self or to nil accordingly.
I was thinking about intermittently freezing, main thread and SDWebImage.
Assuming that you using the image you downloaded from downloadImageWithURL:options:progress:completed:'s completed block .. if so, make sure you dispatch to the main queue before using using the image.
If you use the SDWebImageDownloader directly, the completion block (as you noted) will be invoked on a background queue, you can fix it using dispatch_async on the main queue from the completion.
Otherwise you can use:
SDWebImageManager downloadImageWithURL:options:progress:completed: (method that invokes the completion blocks on the main queue).
if the problem persist (just because you speaking about "..some unique things about my app is that it is very image heavy..") look also Common problems expecially the Handle image refresh know problems.
Add to your check also this nice snippet code:
import UIKit.UINavigationController
public typealias VoidBlock = (Void -> Void)
public extension UINavigationController
{
public func pushViewController(viewController: UIViewController, animated: Bool, completion: VoidBlock) {
CATransaction.begin()
CATransaction.setCompletionBlock(completion)
self.pushViewController(viewController, animated: animated)
CATransaction.commit()
}
}
maybe can help to understand if pushViewController finish, if finish with all viewControllers expected ..
Another test I try to make is to launch the app with iOS 8.x and iPhone 6+, because there are some issues in the pureLayout project around iOS 9. Can you send feedbacks around this test?
I've some suspicious also on the real scrollview dimension before the pushview action, can you analyze the current view by examing the view hierarchy?
Please check if you have any unnecessary codes like below in your BaseNavigationViewController :
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
Here I have a sample code which reproduces this issue (freezing app in pushing VC) that you have to reproduce it by these steps:
Try using the (left | right) edge pop gesture when there are no view controllers below it (i.e on root view controllers, your VC-Home controller)
Try clicking on any UI elements after this(which pushes you to next ViewController).
Sample Code: https://github.com/aliuncoBamilo/TestNavigationPushBug
My case solved by restarting Xcode and simulator, then choosing a different device from simulators list.
Answer by #Penkey Suresh! and #Tim Friedland helped me alot, one thing that i had to do extra for my usecase, might help someone else too.
USE CASE:
I had tab bar controller and i wanted swipe-back-gesture on screens let's say i'm on tabA and opened VC1, VC2 from there. My swipe gesture was working correctly on VC1 and VC2 but for some reason it was not disabling when coming back to tabA which is why it was freezing when trying to swipe left from there and trying to click somewhere (as mentioned by #Pankey Suresh)
SOLUTION IN SWIFT 5
I have this custom class implemented:
class BaseSwipeBack: UIViewController, UIGestureRecognizerDelegate {
func swipeToPop(enable: Bool) {
if enable && (navigationController?.viewControllers.count ?? 0 > 1){
self.navigationController?.interactivePopGestureRecognizer?.delegate = self
navigationController?.interactivePopGestureRecognizer?.isEnabled = true;
}
else {
self.navigationController?.interactivePopGestureRecognizer?.delegate = nil
navigationController?.interactivePopGestureRecognizer?.isEnabled = false;
}
}
}
USAGE
VC1 class - make it sub class of BaseSwipeBack that we just created so that you can access the functions
class VC1: BaseSwipeBack {
override func viewDidLoad() {
}
override func viewDidAppear(_ animated: Bool) {
self.swipeToPop(enable: true)
}
deinit {
self.swipeToPop(enable: false)
}
}
tabAController class - although i have disabled gesture in deiniting VC1 but it was not working fine so i had to disable it again tabAController
class tabAController: BaseSwipeBack {
override func viewDidLoad() {
}
override func viewDidAppear(_ animated: Bool) {
self.swipeToPop(enable: false)
}
}
I want to show a popup menu and without using nib.I dont like using nib since the headache to implement delegate for a simple functionality. I succeeded using modalPresentationStyle as a Popover to show ViewController as a Popover and it works fine with the below code.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var btnShowPopOver: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func showPopUP(sender: AnyObject) {
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let popVC = storyboard.instantiateViewControllerWithIdentifier("pop")as! PopViewController
popVC.modalPresentationStyle = UIModalPresentationStyle.Popover
popVC.preferredContentSize = CGSizeMake(320, 240)
popVC.popoverPresentationController!.delegate = self
let popOverController = popVC.popoverPresentationController
popOverController!.sourceView = sender as! UIView // where to stick the bar item in which view
popOverController!.sourceRect = CGRectMake(70,30, 0, 0) //where to stick the bar
popOverController?.permittedArrowDirections = nil
self.presentViewController(popVC, animated: true, completion: nil)
}
}
extension ViewController :UIPopoverPresentationControllerDelegate {
func adaptivePresentationStyleForPresentationController(PC: UIPresentationController) -> UIModalPresentationStyle {
return UIModalPresentationStyle.None
}
func popoverPresentationControllerDidDismissPopover(popoverPresentationController: UIPopoverPresentationController) {
var controller = popoverPresentationController.presentedViewController as! PopViewController
println(" this is data from pop view controller \(controller.textField.text)")
}
}
Apple doc says
Popover controllers are for use exclusively on iPad devices.
Attempting to create one on other devices results in an exception.
I tested this on real device iphone-6 and its working fine..I am loving this Popover.
Should i use Popover or not as per the apple documentation?Since its working fine in iphone,will my app get rejected for using it later?
Since iOS8 we are now able to create popovers, that will be the same
on iPhone, as on iPad, which would be especially awesome for those who
make universal apps, thus no need to make separate views or code.
Source : UIPopoverController for iphone not working?
Look at this answers :
https://stackoverflow.com/a/14789022/3202193
https://stackoverflow.com/a/30418212/3202193
In the per-release document of iOS 9 also they are saying like :
The UIPopoverController class is used to manage the presentation of content in a popover. You use popovers to present information temporarily. The popover content is layered on top of your existing content and the background is dimmed automatically. The popover remains visible until the user taps outside of the popover window or you explicitly dismiss it. Popover controllers are for use exclusively on iPad devices. Attempting to create one on other devices results in an exception.
EDIT
I think you have used the UIPopoverPresentationController which is Available in iOS 8.0 and later.
The UIPopoverController and UIPopoverPresentationController are two different things provided by Apple.