Swift 在UIToolbar内调整UIBarButtonim标题的垂直位置

Swift 在UIToolbar内调整UIBarButtonim标题的垂直位置,swift,uibarbuttonitem,uitoolbar,Swift,Uibarbuttonitem,Uitoolbar,是否可以在UIToolbar内调整UIBarButtonItem的标题 我尝试了以下代码行,但没有成功: UIBarButtonItem.appearance().setTitlePositionAdjustment(UIOffset(horizontal: 30, vertical: 30), forBarMetrics: UIBarMetrics.Default) 还有这个: self.setTitlePositionAdjustment(UIOffset(horizontal: 30,

是否可以在
UIToolbar
内调整
UIBarButtonItem
的标题

我尝试了以下代码行,但没有成功:

UIBarButtonItem.appearance().setTitlePositionAdjustment(UIOffset(horizontal: 30, vertical: 30), forBarMetrics: UIBarMetrics.Default)
还有这个:

self.setTitlePositionAdjustment(UIOffset(horizontal: 30, vertical: 30), forBarMetrics: UIBarMetrics.Compact)
垂直偏移UIToolBar中的UIBarButtonim(文本!)似乎不适用于Xcode 7(Beta 6)

你是对的,这应该可以做到:
UIBarButtonItem.appearance().setTitlePositionAdjustment

在AppDelegate中全局或全局:

UIBarButtonItem.appearance().setTitlePositionAdjustment(UIOffset(horizontal: 30, vertical: 30), forBarMetrics: UIBarMetrics.Default)
或与项目出口本身一起本地:

(Swift)

(Obj C)


这是一个外观上的bug吗?

我对此也很失望,所以我将uibarbuttonite子类化,以便按预期工作。根据我的回答,我得出以下结论:

@interface TitleAdjustingBarButtonItem(){
    UIView*   _parentView;
    UIButton* _button;
}

@end

@implementation TitleAdjustingBarButtonItem

-(id) initWithTitle:(NSString *)title style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action{
    _button = [[UIButton alloc] init];
    [_button setTitle:title forState:UIControlStateNormal];

    [_button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];

    [_button sizeToFit];

    _parentView = [[UIView alloc] initWithFrame:_button.bounds];

    [_parentView addSubview:_button];

    return [super initWithCustomView:_parentView];
}

#pragma mark - property setters -

-(void) setTitlePositionAdjustment:(UIOffset)adjustment forBarMetrics:(UIBarMetrics)barMetrics{
    [_button sizeToFit];
    _parentView.bounds = CGRectMake(0.0, 0.0, _button.bounds.size.width - (adjustment.horizontal * 2.0), _button.bounds.size.height - (adjustment.vertical * 2.0));
}

诚然,它没有考虑到不同的酒吧指标,但对于默认指标,我已经得到了这一工作相当好

我找到了一个解决办法。我试图更改导航栏中“后退”按钮标题的位置。标题似乎固定在它的左下角。所以我改变了整个导航栏的位置,并相应地调整了它的大小,使它在视图中看起来是相同的大小

    let xDisplacement: CGFloat = -25 // Adjust horizontal position
    let yDisplacement: CGFloat = 9 // Adjust vertical position
    let h = (self.viewIfLoaded?.frame.size.height)!/12 //Set your height in reference to the size of the view
    let w = (self.viewIfLoaded?.frame.size.height)!//Set your width in reference to the size of the view 
//Change placement adjusting size for change.
    self.navigationController?.navigationBar.frame = CGRect(x: xDisplacement, y: yDisplacement, width: w-xDisplacement, height: h-yDisplacement)
在我的例子中,背景是透明的,所以调整并不会真正产生影响。我也试着把标题移到左边。我认为这是唯一一种去调整有用的情况

无论如何,这只是一个解决办法,但我希望它能有所帮助


这里完全没有问题,感谢您的反馈

更改文本的
基线偏移量

let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(self.someFunction))
let attributes = [NSAttributedString.Key.baselineOffset: NSNumber(value: -3)]
doneButton.setTitleTextAttributes(attributes, for: .normal)
负的
baselineOffset
将向下移动标题文本,正值将向上移动标题文本。0是默认值


我在iOS 11上对此进行了测试。

建议:在
UIBarButtonItem
中使用
UIButton

我有一个工具栏,它的两侧有两个较高的按钮(屏幕截图中的“向上”按钮和“向下”按钮),当用户启动一个操作时,我会在工具栏中显示两个只有文本的按钮。当我最初将它们作为
uibarbuttonim
对象放在工具栏上时,它们在工具栏上的位置非常低,我没有尝试过让它们坐得更高。它们的位置使得用户很难在iPadPro设备上按它们,因为设备底部的栏有点挡住了它们

我发现,如果我将
UIButton
拖动到工具栏,IB会自动将它们放在uibarbuttonite中作为视图,并且它们在工具栏中垂直居中。然后,我可以像往常一样调整
ui按钮的内容插入,并且正确地反映了更改。(因为我只是想让它们垂直居中,仅仅使用
ui按钮
对我来说很有效)。记住,您连接的任何iBaction都应该连接到UIButtons

我是用XCode 10.1和swift 4实现的


到底什么不起作用?垂直调整标题的位置如果您尝试设置标题插入以更改标题位置,请尝试使用此设置,希望这能解决您的问题。
setTitleEdgeInsets
确实不是
UIBarButtonItem
上的属性。有人找到办法了吗?iOS 9?水平工作但垂直不做任何更改。你知道为什么吗?我也有同样的问题。当我将工具栏高度更改为44时,barbuttonitems垂直居中。希望它能帮助别人。
    let xDisplacement: CGFloat = -25 // Adjust horizontal position
    let yDisplacement: CGFloat = 9 // Adjust vertical position
    let h = (self.viewIfLoaded?.frame.size.height)!/12 //Set your height in reference to the size of the view
    let w = (self.viewIfLoaded?.frame.size.height)!//Set your width in reference to the size of the view 
//Change placement adjusting size for change.
    self.navigationController?.navigationBar.frame = CGRect(x: xDisplacement, y: yDisplacement, width: w-xDisplacement, height: h-yDisplacement)
let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(self.someFunction))
let attributes = [NSAttributedString.Key.baselineOffset: NSNumber(value: -3)]
doneButton.setTitleTextAttributes(attributes, for: .normal)