LeftbarButtonItem删除填充?

时间:2015-10-27 22:50:34

标签: swift

我想在UINavigationController中使用一个标识作为LeftbarButtonItem,是否可以删除左边的填充?

Ill alreay尝试过:

    let logoView = UIView(frame: CGRectMake(-15, 0, 44, 44))
    logoView.backgroundColor = UIColor.darkGrayColor()

    var leftBarButtonItem: UIBarButtonItem = UIBarButtonItem(customView: logoView)

    self.navigationItem.setLeftBarButtonItem(leftBarButtonItem, animated: false)

但那不起作用。 Ill表示此图中显示的空间:

enter image description here

有什么想法吗?提前谢谢。

这是Swift解决方案:

    let logoView = UIView(frame: CGRectMake(0, 0, 44, 44))
    logoView.backgroundColor = UIColor.darkGrayColor()

    let negativeSpacer = UIBarButtonItem.init(barButtonSystemItem: .FixedSpace, target: nil, action: nil)
    negativeSpacer.width = -20;

    let leftBarButtonItem: UIBarButtonItem = UIBarButtonItem(customView: logoView)

    self.navigationItem.leftBarButtonItems = [negativeSpacer, leftBarButtonItem]

2 个答案:

答案 0 :(得分:8)

以下是删除自定义左按钮项左侧的填充的示例:

UIBarButtonItem *backButtonItem // Assume this exists, filled with our custom view

// Create a negative spacer to go to the left of our custom back button, 
// and pull it right to the edge:
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] 
    initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace 
    target:nil action:nil];
negativeSpacer.width = -5; 

// Note: We use 5 above b/c that's how many pixels of padding iOS seems to add

// Add the two buttons together on the left:
self.navigationItem.leftBarButtonItems = [NSArray 
    arrayWithObjects:negativeSpacer, backButtonItem, nil];

答案 1 :(得分:0)

@Piyush Sharma'Answer'的快速版本

actionBarWidth = self.navigationController?.navigationBar.frame.width as! CGFloat
actionBarHeight = self.navigationController?.navigationBar.frame.height as! CGFloat

actionBarView.frame = CGRect (x: 0, y: 0, width: actionBarWidth, height: actionBarHeight)

 let emptySpace = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
 emptySpace.width = -16
 let leftBarBtn = UIBarButtonItem(customView: actionBarView)
 self.navigationItem.leftBarButtonItems = [emptySpace,leftBarBtn]