如何调整导航条上的leftBarButtonItem和rightBarButtonItem的位置

      最近发现一些开发ios的新朋友在碰到调整导航条上leftBarButtonItem和rightBarButtonItem的位置的问题显得非常棘手 , 我上网查了下发现网上对这种问题的回答或者提出的解决方案并不是很好,而且很多的都是根本就无法达到自由调整左右按钮位置的。下面我以leftBarButtonItem为例:

设想这类问题的出处:(美女的诱惑)

自定义leftBarButtonItem和rightBarButtonItem 同时为了更好体现封装性新建一个分类来封装好button方法

wKiom1X76xKw9DHpAAIOESuLt3U976.jpg

然后我在NavigationController中来设置:

wKiom1X77TTzG13BAAKLZ19OME4524.jpg

本来这样是没有什么问题的 但是如果切图的美女把leftBarButtonItem搞的有点不正常 结果就这样悲剧了:

wKioL1X78H7xmCwrAAAhu6fo_I0961.jpg

左右不对称,这搞什么嘛 这让搞开发的情何以堪啊,当然了我们开发人员看见了还不算最差,一定不要让我的上帝(用户)看见才行。有人就说这个问题还不简单, 叫切图的那位美女重新搞搞不就行了 我直接跟你说吧 切图美女说这张图完全没问题 就是这样的,爱要不要,爱搞不搞。好吧 我表示真的有气魄,有内涵。

那么现在问题来了 在不搞切图美女不搞图片的情况下搞,搞是可以搞,但是非常难搞,说罢 ,现在就开始搞。

分类弄了就不要浪费了 重新搞下leftBarButtonItem就行

wKioL1X786vAxxBoAAQfvxncuWQ720.jpg

wKiom1X78dDDk9apAAAv6wt8ECs794.jpg

这好像就搞定了

重点:

这里需要提到BarButtonSystemItem的样式- UIBarButtonSystemItemFixedSpace

我不多说 请看官方解释:Blank space to add between other items. Only the width property is used when this value is set.  反正我是看不懂 。再来说说negativeSpacer.width = 0 表示leftBarButtonItem的x离屏幕的左边还有15像素 同样表示rightBarButtonItem的CGRectGetMaxX()离屏幕的右边还有15像素 那么好像我们就可以通过negativeSpacer.width来调整leftBarButtonItem的位置。搞了老半天原来就是得靠这家伙。但有一点:negativeSpacer.width赋值负数对于leftBarButtonItem来说是左移多少像素 对于rightBarButtonItem来说是右移多少像素  反之亦然.

说了这么多 我只想说一句 切图的美女 你对我好点 好不好?


最后附上BarButtonSystemItem的系统样式:

typedef enum {   UIBarButtonSystemItemDone,   UIBarButtonSystemItemCancel,   UIBarButtonSystemItemEdit,   UIBarButtonSystemItemSave,   UIBarButtonSystemItemAdd,   UIBarButtonSystemItemFlexibleSpace,   UIBarButtonSystemItemFixedSpace,   UIBarButtonSystemItemCompose,   UIBarButtonSystemItemReply,   UIBarButtonSystemItemAction,   UIBarButtonSystemItemOrganize,   UIBarButtonSystemItemBookmarks,   UIBarButtonSystemItemSearch,   UIBarButtonSystemItemRefresh,   UIBarButtonSystemItemStop,   UIBarButtonSystemItemCamera,   UIBarButtonSystemItemTrash,   UIBarButtonSystemItemPlay,   UIBarButtonSystemItemPause,   UIBarButtonSystemItemRewind,   UIBarButtonSystemItemFastForward,   UIBarButtonSystemItemUndo ,        // iOS 3.0 and later   UIBarButtonSystemItemRedo ,        // iOS 3.0 and later   UIBarButtonSystemItemPageCurl ,    // iOS 4.0 and later } UIBarButtonSystemItem;

Constants

  • UIBarButtonSystemItemDone

    The system Done button. Localized. image: ../Art/UIBarButtonSystemItemDone.png

    Available in iOS 2.0 and later.

  • UIBarButtonSystemItemCancel

    The system Cancel button. Localized. image: ../Art/UIBarSystemItemCancel.png

    Available in iOS 2.0 and later.

  • UIBarButtonSystemItemEdit

    The system Edit button. Localized. image: ../Art/UIBarSystemItemEdit.png

    Available in iOS 2.0 and later.

  • UIBarButtonSystemItemSave

    The system Save button. Localized. image: ../Art/UIBarButtonSystemItemSave.png

    Available in iOS 2.0 and later.

  • UIBarButtonSystemItemAdd

    The system plus button containing an icon of a plus sign. image: ../Art/UIBarButtonAdd.pdf

    Available in iOS 2.0 and later.

  • UIBarButtonSystemItemFlexibleSpace

    Blank space to add between other items. The space is distributed equally between the other items. Other item properties are ignored when this value is set.

    Available in iOS 2.0 and later.

  • UIBarButtonSystemItemFixedSpace

    Blank space to add between other items. Only the width property is used when this value is set.

    Available in iOS 2.0 and later.

  • UIBarButtonSystemItemCompose

    The system compose button. image: ../Art/UIBarButtonCompose.pdf

    Available in iOS 2.0 and later.

  • UIBarButtonSystemItemReply

    The system reply button. image: ../Art/UIBarButtonReply.pdf

    Available in iOS 2.0 and later.

  • UIBarButtonSystemItemAction

    The system action button. image: ../Art/UIBarButtonAction.pdf

    Available in iOS 2.0 and later.

  • UIBarButtonSystemItemOrganize

    The system organize button. image: ../Art/UIBarButtonOrganize.pdf

    Available in iOS 2.0 and later.

  • UIBarButtonSystemItemBookmarks

    The system bookmarks button. image: ../Art/UIBarButtonBookmarks.pdf

    Available in iOS 2.0 and later.

  • UIBarButtonSystemItemSearch

    The system search button. image: ../Art/UIBarButtonSearch.pdf

    Available in iOS 2.0 and later.

  • UIBarButtonSystemItemRefresh

    The system refresh button. image: ../Art/UIBarButtonRefresh.pdf

    Available in iOS 2.0 and later.

  • UIBarButtonSystemItemStop

    The system stop button. image: ../Art/UIBarButtonStop.pdf

    Available in iOS 2.0 and later.

  • UIBarButtonSystemItemCamera

    The system camera button. image: ../Art/UIBarButtonCamera.pdf

    Available in iOS 2.0 and later.

  • UIBarButtonSystemItemTrash

    The system trash button. image: ../Art/UIBarButtonTrash.pdf

    Available in iOS 2.0 and later.

  • UIBarButtonSystemItemPlay

    The system play button. image: ../Art/UIBarButtonPlay.pdf

    Available in iOS 2.0 and later.

  • UIBarButtonSystemItemPause

    The system pause button. image: ../Art/UIBarButtonPause.pdf

    Available in iOS 2.0 and later.

  • UIBarButtonSystemItemRewind

    The system rewind button. image: ../Art/UIBarButtonRewind.pdf

    Available in iOS 2.0 and later.

  • UIBarButtonSystemItemFastForward

    The system fast forward button. image: ../Art/UIBarButtonFastForward.pdf

    Available in iOS 2.0 and later.

  • UIBarButtonSystemItemUndo

    The system undo button. image: ../Art/UIBarButtonSystemItemUndo.png

    Available in iOS 3.0 and later.

  • UIBarButtonSystemItemRedo

    The system redo button. image: ../Art/UIBarButtonSystemItemRedo.png

    Available in iOS 3.0 and later.

  • UIBarButtonSystemItemPageCurl

    The system page curl button. image: ../Art/UIBarButtonSystemItemPageCurl.png

    This bar button image can be used only for bar button items placed on toolbars.

    Available in iOS 4.0 and later.




negativeSpacer

你可能感兴趣的:(ios)