swift - Change Spacing Between UIBarButtonItems in iOS 8 -
i have uinavigationitem
on view controller, , trying cut down spacing between 2 rightbarbuttonitems
. here of code:
// create 2 uibarbuttonitems allow item1:uibarbuttonitem = uibarbuttonitem(customview: view1) allow item2:uibarbuttonitem = uibarbuttonitem(customview: view2) var fixedspace:uibarbuttonitem = uibarbuttonitem(barbuttonsystemitem: uibarbuttonsystemitem.fixedspace, target: nil, action: nil) fixedspace.width = -20.0 // add together rightbarbuttonitems on navigation bar viewcontroller.navigationitem.rightbarbuttonitems = [item2, fixedspace, item1]
as can seen, using fixedspace uibarbuttonitem
, not changing spacing reason. have thought subclassing either uinavigationitem
or uibarbuttonitem
can set spacing accordingly, couldn't seem find methods override alter spacing between items.
any insight on how solve problem appreciated!
thanks @fogmeister's help, figured out width of view1
, view2
objects, uibuttons
, large. why there abnormal spacing between them. here final code:
// first button's image var view1img:uiimage = uiimage(named: "image1")! // create first button var view1:uibutton = uibutton(frame: cgrect(x: 0, y: 0, width: view1img.size.width, height: view1img.size.height)) // sec button's image var view2img:uiimage = uiimage(named: "image2")! // create sec button var view2:uibutton = uibutton(frame: cgrect(x: 0, y: 0, width: view2img.size.width, height: view2img.size.height)) // create 2 uibarbuttonitems allow item1:uibarbuttonitem = uibarbuttonitem(customview: view1) allow item2:uibarbuttonitem = uibarbuttonitem(customview: view2) // set 26px of fixed space between 2 uibarbuttonitems var fixedspace:uibarbuttonitem = uibarbuttonitem(barbuttonsystemitem: uibarbuttonsystemitem.fixedspace, target: nil, action: nil) fixedspace.width = 26.0 // set -7px of fixed space before 2 uibarbuttonitems aligned border var negativespace:uibarbuttonitem = uibarbuttonitem(barbuttonsystemitem: uibarbuttonsystemitem.fixedspace, target: nil, action: nil) negativespace.width = -7.0 // add together rightbarbuttonitems on navigation bar viewcontroller.navigationitem.rightbarbuttonitems = [negativespace, item2, fixedspace, item1]
i create background image first uibutton
, utilize size create frame uibutton
. perform same actions sec uibutton
. then, create uibarbuttonitems
2 uibuttons
. after that, create 26px of fixed space , -7.0px of fixed space. purpose of former create amount of space between 2 buttons. purpose of latter move uibarbuttonitems
on right. then, add together of uibarbuttonitems
rightbarbuttonitems
in particular order want.
it works great now! of help, fogmeister!
ios swift ios8 uinavigationitem spacing
No comments:
Post a Comment