Infamous911
Infamous911

Reputation: 1531

Xcode - Width and Horizontal Position Are Ambiguous

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:

viewController

The arrow and home buttons do not have any constraints. The constraints for the larger gray view is as follows:

gray view constraints

The constraints for the "Authors" label is as follows:

Authors label constraints

And the constraints for the blue thin view is as follows:

blue thin view constraints

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

Answers (2)

Mike Taverne
Mike Taverne

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.

  1. Embed the Authors view controller in a navigation controller.
  2. Create a Show segue from a button on the Home view controller to the navigation controller that contains the Authors view controller.
  3. 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)
    
  4. 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

Bishow Gurung
Bishow Gurung

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:

  1. You are giving "bottom space to:", its not necessary as it is maintained by "height equals:2"
  2. you are giving "leading space to" and "Align to x" thats too don't make sense.

Solution:

Just do this->

  1. Give "leading space to: superview(greyview)" and "Trailing space to: superview(greyview)" both value 0
  2. Give "top space to:(authors)" certain value
  3. And at last "Height Equals to: 2" or as you wish.

enter image description here

Constraints of greyView

enter image description here

Constraints of label

enter image description here

Constraints of Blue bar

enter image description here

Upvotes: 1

Related Questions