5

Xcode is not allowing me to put constraints on my BarButtonItem on a ViewController in the MainStoryBoard. It's appearing too far to the left where it's unreadable as shown in the image.

The image below shows where the BarButtonItem is. It's supposed to say "Item". I also looked on the preview split screen on Xcode and it looked fine for the iPhone6 but not for the iPhone 4 (it's half cut on the iPhone 4 but here its 3/4s cut). Here I'm running for the iPhone 6 and i get that.

How do I add constraints in Xcode or how do I add the constraints programmatically to this BarButtonItem?

enter image description here

Any help is much appreciated.

Lukesivi
  • 2,206
  • 4
  • 25
  • 43
  • what constraint do you want to add? top constraint or? – Orkhan Alizade Oct 19 '15 at 09:45
  • I want to position the BarButtonItem more to the right. @OrkhanAlizade – Lukesivi Oct 19 '15 at 09:46
  • are u add nav bar like element in storyboard? if yes - just add constraint not to the elements inside but to the navBar – hbk Oct 19 '15 at 09:48
  • How are you adding the UIBarButtonItem to your nav bar? With interface builder or code? If it's code could you post it? – Chris Byatt Oct 19 '15 at 09:51
  • @Kirill I tried that. It still is too far to the left. I'm adding the UIBarButtonItem with the interface builder. – Lukesivi Oct 19 '15 at 09:54
  • need more information for resolving ur issue - can u share u project – hbk Oct 19 '15 at 09:55
  • @rinyfo4 if you are using IB then it should lock it into place. IB only allows a `UIBarButtonItem` in a specific place. Are you changing the frame or bounds or anything like that? Are you definitely adding a `UIBarButtonItem` and not a `UIButton`? – Chris Byatt Oct 19 '15 at 09:57
  • @ChrisByatt yes, it's definitely a `UIBarButtonItem` on a `Navigation Bar` in a viewController, (not a `Navigation Controller). Should I get rid of it as an IBOutlet? – Lukesivi Oct 19 '15 at 10:01
  • I can share the project, but it's a MainStoryBoard issue. I just remade a new project to see if i was crazy, and the same thing happened. This time, the BarButtonItem appeared, but it was still too far to the left. You can recreate it by just adding the BarButtonItem to a nav-bar in a ViewController in the MainStoryBoard. I'll share the project if not. Let me know. Thanks @Kirill – Lukesivi Oct 19 '15 at 10:03
  • @rinyfo4 this is my result in IB when I add a bar button item to a nav bar https://gyazo.com/ba9d0b3e4fc0e1f06b404d338aff61b4 – Chris Byatt Oct 19 '15 at 10:17
  • It looks the same for me. Until i run it. When i run it it looks like it does above. @ChrisByatt – Lukesivi Oct 19 '15 at 10:18
  • @rinyfo4 on which device? https://gyazo.com/acd3edec290b154fffff6058461d8561 – Chris Byatt Oct 19 '15 at 10:20
  • iPhone 6. In your image #2, how would you make "Item" lower? That's my issue. I can't move it, right left up or down... @ChrisByatt – Lukesivi Oct 19 '15 at 10:22
  • @rinyfo4 http://stackoverflow.com/questions/5761183/change-position-of-uibarbuttonitem-in-uinavigationbar – Chris Byatt Oct 19 '15 at 10:24
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/92713/discussion-between-rinyfo4-and-chris-byatt). – Lukesivi Oct 19 '15 at 10:24

2 Answers2

4

Drag and drop navigation bar in your xib / story board

enter image description here

U can add constraints to this navigation bar as per your requirement.

Then add Bar Button Item to your navigation bar

enter image description here

Bar Button Item takes it default position u cannot incorporate autolayout there.

Although if u want positions of Bar Button Item Fixed or Flexible u can make use of Fixed Space Bar Button Item or Flexible Space Bar Button Item.

enter image description here

If positions are fixed drag Fixed Space Bar Button between your two Bar Button Item's. And if positions is not fixed its flexible drag Flexible Space Bar Button Item between your two Bar Button Item's.

Happy Coding.. :)

luckyShubhra
  • 2,731
  • 1
  • 12
  • 19
-2

Try this:

let rightConstraint = NSLayoutConstraint(item: your_item_here, attribute: .Right, relatedBy: .Equal, toItem: your_item_here, attribute: .Left, multiplier: 0, constant: 1)
your_item_here.addConstraint(rightConstraint)

P.S if you want to add constraint to your UIBarButtonItem, it will rise an error

Value of type 'UIBarButtonItem' has no member 'addConstraint'

P.S.S

Because of it is not a view class, you can't apply constraints to it.

Orkhan Alizade
  • 7,379
  • 14
  • 40
  • 79