0

Im having a problem with the UIView margins when Instantiating it on the callback function. When the view is instantiated like this it looks normal:

let viewController = UIStoryboard(name: "Tracking", bundle: nil).instantiateViewControllerWithIdentifier("tracking") as! CarrierTrackingVC
elDrawer.mainViewController = viewController

Normal Screen

But when I instantiate in the api request callback like this, the view looks weird

TrackingController().getTruckTrack("7RZEY3VP") { (response, errs) in
   if !self.requestErrors(errs) {
      let truckTrack = TruckTrack(json:response["truck_track"].description)
      let viewController = UIStoryboard(name: "Tracking", bundle: nil).instantiateViewControllerWithIdentifier("tracking") as! CarrierTrackingVC
      elDrawer.mainViewController = viewController
   }
}

Weird margins Screen

I would like to know why that happen and any clue of how could I fix it. Thanks.

EDIT: This is the full code:

import Foundation
import UIKit
import KYDrawerController
import PKHUD
import JLToast

class MenuVC: UITableViewController {

@IBOutlet weak var fullnameLBL: UILabel!
@IBOutlet weak var profileTypeLBL: UILabel!
@IBOutlet weak var usernameLBL: UILabel!
@IBOutlet weak var profilePicIMG: RoundedImage!

@IBOutlet weak var homeLBL: UILabel!
@IBOutlet weak var servicesLBL: UILabel!
@IBOutlet weak var signOutLBL: UILabel!
@IBOutlet weak var trackingLBL: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
    initContent()
    NSUserDefaults.standardUserDefaults().addObserver(self, forKeyPath: PICTURE, options: NSKeyValueObservingOptions.New, context: nil)
    NSUserDefaults.standardUserDefaults().addObserver(self, forKeyPath: NAME, options: NSKeyValueObservingOptions.New, context: nil)
    if let url = SessionManager.sharedInstance.picture {
        profilePicIMG.imageFromUrl(url)
    }
}

override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
    if keyPath == PICTURE {
        if let url = object as? String {
            profilePicIMG.imageFromUrl(url)
        }
    }
    if keyPath == NAME {
        usernameLBL.text = object as? String
    }
}

deinit {
    NSUserDefaults.standardUserDefaults().removeObserver(self, forKeyPath: PICTURE)
    NSUserDefaults.standardUserDefaults().removeObserver(self, forKeyPath: NAME)
}

override func tableView(tableView: UITableView, didSelectRowAtIndexPath newIndexPath: NSIndexPath) {
    self.tableView.deselectRowAtIndexPath(newIndexPath, animated: true)
    let elDrawer = (self.navigationController?.parentViewController as! KYDrawerController)

    switch newIndexPath.row {
    //Profile info
    case 0:
        break
    //Home
    case 1:
        elDrawer.mainViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("MainNavigation")
    //Services
    case 2:
        elDrawer.mainViewController = UIStoryboard(name: "Services", bundle: nil).instantiateViewControllerWithIdentifier("services")
    case 3:
        TrackingController().getTruckTrack("7RZEY3VP") { (response, errs) in
            if !self.requestErrors(errs) {
                let truckTrack = TruckTrack(json:response["truck_track"].description)
                let viewController = UIStoryboard(name: "Tracking", bundle: nil).instantiateViewControllerWithIdentifier("tracking") as! CarrierTrackingVC
                viewController.truckTrack = truckTrack
                elDrawer.mainViewController = viewController
            }
        }
        /*let viewController = UIStoryboard(name: "Tracking", bundle: nil).instantiateViewControllerWithIdentifier("tracking") as! CarrierTrackingVC
        viewController.truckTrack = TruckTrack()
        elDrawer.mainViewController = viewController*/
    default:
        signnOut()
    }

    elDrawer.setDrawerState(.Closed, animated: true)
}

func signnOut() {
    HUD.show(.LabeledProgress(title: NSLocalizedString("SIGNING_OUT", comment: ""), subtitle: nil))
    UserController().signOut { (response, err) in
        HUD.hide()
        self.changeRootViewControllerWithIdentifier("start",storyboard: "Main")
    }
}


func initContent() {
    fullnameLBL.text = SessionManager.sharedInstance.userFullName
    usernameLBL.text = SessionManager.sharedInstance.username
    profileTypeLBL.text = SessionManager.sharedInstance.profileType
}

}

EDIT: viewController is a UITabBarController, each Tab contains a Navigation Controller and it's child is a View Controller.

xpiero
  • 65
  • 7
  • you should provide more code. – aircraft Jan 18 '17 at 14:06
  • Is the callback on the main thread? – Matic Oblak Jan 18 '17 at 14:06
  • @aircraft I provided the full code. – xpiero Jan 18 '17 at 14:15
  • @MaticOblak yes. I provided the full code – xpiero Jan 18 '17 at 14:16
  • @MaticOblak Im using Alamofire, I think it handles the async tasks. – xpiero Jan 18 '17 at 14:22
  • Well if you are sure both are executed on the main thread then there should be no difference between the two her. Is it possible some other part of the code is responsible for this? Anyway you can put a breakpoint into the call and see what thread it is executed on. Where do margins come from anyway? What is the view structure? Have you tried to debug it with that built-in tool that shows you the whole view hierarchy? Also I assume you are getting no constraint warnings in runtime in any of the cases. – Matic Oblak Jan 18 '17 at 14:25
  • @MaticOblak I get this on the console: Unbalanced calls to begin/end appearance transitions for . – xpiero Jan 18 '17 at 14:34
  • I believed that can be caused when you transition to another view controller before the previous transition is finished. Like a.present(b, animated: true) b.present(c, animated: true). Is it possible something similar is happening in your case by mistake? Just to test it in the block try this: DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) { // call the view controller stuff in here } – Matic Oblak Jan 18 '17 at 14:42
  • @MaticOblak I could get rid of the animations warning by setting viewWillAppear false on the target view but I still get the weird margins. I also tried your async approach but didn't work – xpiero Jan 18 '17 at 14:51
  • What is this elDrawer.mainViewController anyway? Both of them. Is there a difference of the drawer state? For instance in first case the mainViewController is nil and the controller is drawn correctly but then in second case it already has a controller there. – Matic Oblak Jan 18 '17 at 14:55
  • In both cases elDrawer.mainViewController has the HomeVC in first instance, then I open the drawer and click the Tracking option wich is the case 3 of that switch [link](http://oi63.tinypic.com/1zhcgj.jpg). I really don't understand why that happens – xpiero Jan 18 '17 at 15:14

1 Answers1

0

It looks as if the vc's view is being covered by the navigation bar, which, prior to OS 6 or 7, was the default. I can't explain why the behavior would appear in one instantiation context vs the other, but you fix by setting explicitly with:

let viewController = // yada yada
viewController.edgesForExtendedLayout = UIRectEdgeNone

EDIT if I'm right that viewController is a navigation view controller, then we need it's root to adjust the edge property...

let viewController = /*yada yada*/ as! UINavigationController
let rootVC = viewController.viewControllers[0]
rootVC.edgesForExtendedLayout = UIRectEdgeNone
danh
  • 62,181
  • 10
  • 95
  • 136
  • Do you know the class of "viewController"? I just took a second look at the question, and I think it may be a UINavigationController. If that's the case, then my idea here is wrong. We want to change the edge property for the navigation vc's root view controller. That can be gotten in a few ways, but lets first confirm that it is indeed a nav vc. – danh Jan 18 '17 at 17:53
  • @xpiero, Edited to illustrate. – danh Jan 18 '17 at 17:58
  • viewController is a UITabBarController, each Tab contains a Navigation Controller. https://i.stack.imgur.com/9j4vg.png – xpiero Jan 18 '17 at 18:51
  • @xpiero, so one of the tabVC's tabs is a navVC, and it's root is a view controller? We want to set the property on that navBar's root view controller. If you have a view controller subclass for that nav VC's root vc, then set it on `self.edgesForExtendedLayout` inside of `viewDidLoad` in your subclass – danh Jan 18 '17 at 20:02
  • yea its a view controller and I did the self.egdesForExtendedLayout and it pushed the top margin, but its no behaving normal, the navbar button is still having wrong margin and when I open the drawer the left and the right margin disappear – xpiero Jan 19 '17 at 11:15