Reputation: 1531
I'm trying to figure out how autolayout works and I have run into a problem where I don't understand why I am getting a warning that "The width and horizontal position are ambiguous" for a view. The view is the bottom thin blue line as you can see in the screenshot of the ViewController I am dealing with:
The arrow and home buttons do not have any constraints. The constraints for the larger gray view is as follows:
The constraints for the "Authors" label is as follows:
And the constraints for the blue thin view is as follows:
I don't understand why I would be getting this warning since the larger gray view isn't getting the warning but there doesn't seem to be any difference in how I treat the two views. I made sure to include constraints on leading and trailing space for both of them so why is the horizontal position ambiguous for the thin blue one? I can kind of see why the width might be ambiguous but I don't understand why it wouldn't be ambiguous for the larger gray view. Shouldn't autolayout automatically deal with it? I want the width obvious to scale with the screen size.
Upvotes: 2
Views: 12088
Reputation: 9352
Remove Align Center X constraint from the blue thin view.
Side note: it looks like you are trying to create a standard navigation bar. If so, there is a better way to do this:
Select the controller you want as the root of your navigation flow (your "Home" controller, perhaps). Then go to Editor > Embed In > Navigation Controller. This will put your view controller inside a navigation controller. You can then wire up a Show segue to your Authors controller, and it will automatically have a navigation bar. At runtime the back arrow to Home will appear automatically.
EDIT:
Say you have Home and Authors view controllers.
Within your Authors view controller viewDidLoad
, add a Home button to dismiss the Authors view controller, like this:
self.navigationItem.setLeftBarButtonItem(UIBarButtonItem(title: "Home", style: UIBarButtonItemStyle.Bordered, target: self, action: "goHome"), animated: true)
Add a function to dismiss your Authors view controller when the Home button is tapped:
func goHome() {
self.navigationController!.dismissViewControllerAnimated(true, completion: nil)
}
You would need to do a little extra to get the arrow icon on your Home button. This post gives some ideas how to do this. If you already have the image, it should be very easy.
Now your Authors view controller is within a real navigation controller, and you can navigate from it to other view controllers, say if user taps an author to get more information.
Upvotes: 0
Reputation: 2010
Its better not to put such view inside the top bar, just below the greybar would be best for it if your are doing it just for partition. But I have resolved for your way Here.
For blue bar:
Solution:
Just do this->
Constraints of greyView
Constraints of label
Constraints of Blue bar
Upvotes: 1