Reputation: 77
I have a UIViewController (named Friends) with a tableview and I want to display views above the Controller's view when the user is offline. This is how I create my views
class func showOffline(host: UIViewController) {
// showOffline image
var disconnectedImage = UIImage(named: "offlineIconGrey")
disconnectedImage = disconnectedImage?.imageWithRenderingMode(.AlwaysOriginal)
let imageView = UIImageView(image: disconnectedImage)
imageView.center = CGPoint(x: host.view.center.x,
y: host.view.center.y - imageView.frame.height/2)
// showOffline Label
let Label = UILabel(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
let labelText = "You appear to be offline \n Please connect to the internet"
Label.center = CGPoint(x: host.view.center.x,
y: imageView.center.y + 3*imageView.frame.height/4)
Label.numberOfLines = 0
Label.text = labelText
Label.textAlignment = .Center
Label.font = UIFont(name: (Label.font?.fontName)!, size: 14.0)
Label.sizeToFit()
Label.textColor = UIColor.whiteColor()
Label.lineBreakMode = .ByCharWrapping
Label.frame.offsetInPlace(dx: -Label.frame.size.width/2, dy: 0)
host.view.insertSubview(imageView, aboveSubview: host.view)
host.view.insertSubview(Label, aboveSubview: host.view)
}
Now it is my understanding, that the host view (host.view) should be the topmost view as that is shown on my storyboard; however, when I call my showOffline function like so
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
if !Reachability.isConnected() {
state = .Offline
} else {
state = .Default
}
}
where offline state calls,
General.showOffline(self)
like so
var state: State = .Default {
didSet {
switch (state) {
case .Default:
print("ViewMode = Default")
//query = ParseHelper.allUsers(updateList)
case .Search:
let searchText = searchBar?.text ?? ""
print(searchText)
//query = ParseHelper.searchUsers(searchText, completionBlock:updateList)
case .Offline:
General.showOffline(self)
searchBar.userInteractionEnabled = false
query = nil
users = [PFUser]() ?? []
print("ViewMode = Offline")
}
}
}
the views do not appear as the topmost views unless I am returning to the view controller (Friends) from a show segue. Otherwise, navigating to that view controller does not show the labels when it should? Am I missing something? I have tried to fix the problem by placing the views above the tableview but in that case the math for centering the views is off, probably because I am not loading any cells and I have a footer view as a UIView. I also tried adding the views to the tableview's footer view, but that didn't work either. What I DON'T want is to add the views to the navigation controller's view as I do not want that behavior, though I will do that if I have no other option.
Can someone please tell me what I am doing wrong or refer me to information that may help me fix my problem? Thanks!
Upvotes: 0
Views: 72
Reputation: 17710
When you call insertSubview:aboveSubview:
, the view you give as second parameter must be a subview of the view you're inserting in. Here, it's the same view, so obviously it won't work.
Unless there are other views that should remain above the ones you're inserting, the easiest way is to just use addSubview:
, which will make the new view topmost among the child of the target view.
Upvotes: 0
Reputation: 52530
host.view.insertSubview(imageView, aboveSubview: host.view)
This cannot work. You add a subview to host.view. The "aboveSubview" must be a subview of host.view. There are other methods for g adding a subview.
Upvotes: 1