Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SnackbarController fails to layout its rootViewController's height correctly #612

Closed
timoschwarzer opened this issue Nov 26, 2016 · 19 comments
Assignees

Comments

@timoschwarzer
Copy link

timoschwarzer commented Nov 26, 2016

My setup is:

  • Window
    • NavigationDrawerController
      • ToolbarController
        • SnackbarController
          • my view controllers

I'm changing my view controllers by calling .transition(to: targetViewController) on the SnackbarController.
The problem I have is that my view controllers have the wrong height.

I've debugged some bounds:

// Screen's bounds
print(Screen.bounds)
// The SnackbarController's view's bounds
print(toolbarController?.rootViewController.view.bounds as! CGRect)
// The bounds of my view controller's view
print((toolbarController?.rootViewController as! AppSnackbarController).rootViewController.view.bounds)

The output was:

(0.0, 0.0, 768.0, 1024.0)
(0.0, 0.0, 768.0, 955.0)
(0.0, 0.0, 768.0, 1162.0) // This should be 955, right?

And it even becomes more weird: If change my view controller using .transition, the new view controller's height increases by 138 everytime. (It's 138 on all devices)
So if I change my view controller, it outputs:

(0.0, 0.0, 768.0, 1024.0)
(0.0, 0.0, 768.0, 955.0)
(0.0, 0.0, 768.0, 1300.0)

And if I change it again, it outputs:

(0.0, 0.0, 768.0, 1024.0)
(0.0, 0.0, 768.0, 955.0)
(0.0, 0.0, 768.0, 1438.0)

Maybe you can take a look...
Thanks!

  • Timo
@timoschwarzer
Copy link
Author

timoschwarzer commented Nov 26, 2016

When I comment out (RootController.swift : 129)

viewController.view.frame = rootViewController.view.frame

the SnackbarController's view doesn't get larger when changing view controllers. But it's still too large.

@daniel-jonathan
Copy link
Member

I will take a look :)

@daniel-jonathan
Copy link
Member

daniel-jonathan commented Nov 27, 2016

Is the issue that your views are behind the TabBarController ? If so, this is not actually a Material issue, but iOS in general. You need to set the edges of the view controller. Set in your rootViewController this property:

edgesForExtendedLayout = []

@timoschwarzer
Copy link
Author

The issue is, that the rootViewController of my SnackbarController has always the wrong height. And the height gets increased by 138 every time I change the rootViewController using .transition()

@daniel-jonathan
Copy link
Member

I understand that. The frame doesn't get increased by any factor, rather it is made the same as the previous that is being transitioned from.

viewController.view.frame = rootViewController.view.frame

If you can share a sample that replicates this, that would be great. You are placing this inside a TabBarController, which does alter the size of UIViewControllers that are within it. Now I am not blaming that, but it is the only item that stands our right away. The RootController manages all navigation controller types (ToolbarController, MenuController, PageTabBarController, SnackbarController, etc... ) So I would suspect that the issue would manifest in all those too if there was an increase being set within the RootController.

This is where a sample project that replicates the issue would be very helpful.

@timoschwarzer
Copy link
Author

I'll make one! :)

@daniel-jonathan
Copy link
Member

:) Thank you!

@timoschwarzer
Copy link
Author

Okay, this is becoming strange... But maybe you do know what happens here:

I'm adding an ActivityIndicator as the Toolbar's rightView like this:

let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .white)
let activityIndicatorContainer = UIView(frame: CGRect())

toolbar.rightViews = [activityIndicatorContainer]

activityIndicatorContainer.autoresizesSubviews = true
activityIndicatorContainer.addSubview(activityIndicator)

activityIndicator.translatesAutoresizingMaskIntoConstraints = false
activityIndicatorContainer.addConstraint(NSLayoutConstraint(item: self.activityIndicator, attribute: .centerX, relatedBy: .equal, toItem: activityIndicatorContainer, attribute: .centerX, multiplier: 1, constant: 0))
activityIndicatorContainer.addConstraint(NSLayoutConstraint(item: self.activityIndicator, attribute: .centerY, relatedBy: .equal, toItem: activityIndicatorContainer, attribute: .centerY, multiplier: 1, constant: 0))

activityIndicator.startAnimating()

When I remove that part, we can completely forget that 138 thing, because it doesn't happen without that. (But I really want to know what happens there and how to add a vertical centered ActivityIndicator to a Toolbar)

But the view stills seems to be too high. I'm setting a tableView's background view like this (should be v- and h-centered):

let messageLabel = UILabel(frame: CGRect(x: 0, y: 0, width: self.tableView.bounds.size.width, height: self.tableView.bounds.size.height))
messageLabel.text = message!
messageLabel.textColor = Color.black
messageLabel.numberOfLines = 0;
messageLabel.textAlignment = .center;
messageLabel.font = UIFont(name: "TrebuchetMS", size: 15)
messageLabel.sizeToFit()
            
self.tableView.backgroundView = messageLabel;
self.tableView.separatorStyle = .none;

This is NavigationDrawerController > ToolbarController > SnackbarController > MyTableViewController:
image

And this is NavigationDrawerController > ToolbarController > PageTabbarController > MyTableViewController
image

@timoschwarzer
Copy link
Author

And here is an example project. See AppDelegate.swift, line 41

NavigationDrawerController copy.zip

@daniel-jonathan
Copy link
Member

I will take a look tomorrow :)

@daniel-jonathan
Copy link
Member

Looking at this now :)

@timoschwarzer
Copy link
Author

Very nice, thank you!

@daniel-jonathan
Copy link
Member

daniel-jonathan commented Nov 30, 2016

So it all works :) I don't think you were showing the snackbar. Add this: animate(snackbar: .visible) to the AppSnackbarController class

The Snackbar is hidden by default.

@timoschwarzer
Copy link
Author

The snackbar is not the problem. The problem is the snackbarController laying out its subviews (see the images above)

@timoschwarzer
Copy link
Author

The first "Hello" should be centered, too, but it isn't. It's an UILabel as a TableView's backgroundView. And the TableView is inside a SnackbarController. If I put the TableView into another Controller, let's say a PageTabBarController (image #2), it is centered.

@daniel-jonathan
Copy link
Member

daniel-jonathan commented Dec 1, 2016

Fixed the issue :) Will make a release momentarily.

@daniel-jonathan
Copy link
Member

Please update to Material 2.3.21 :) It should be aligning correctly now. Thanks for catching this issue.

@timoschwarzer
Copy link
Author

Wow, nice! Thank You! :-)
I will try this soon...

@timoschwarzer
Copy link
Author

Yes, it works! Thanks! :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants