在UIToolbar中调整UIBarButtonItem标题的垂直位置

时间:2015-05-08 12:47:40

标签: swift uibarbuttonitem uitoolbar

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

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

UIBarButtonItem.appearance().setTitlePositionAdjustment(UIOffset(horizontal: 30, vertical: 30), forBarMetrics: UIBarMetrics.Default)

这一个:

self.setTitlePositionAdjustment(UIOffset(horizontal: 30, vertical: 30), forBarMetrics: UIBarMetrics.Compact)

5 个答案:

答案 0 :(得分:4)

垂直偏移UIToolBar中的UIBarButtonItem(Text!)似乎无法与Xcode 7(Beta 6)一起使用。

你是对的,according to the docs,这应该这样做: UIBarButtonItem.appearance().setTitlePositionAdjustment

在AppDelegate中全局:

UIBarButtonItem.appearance().setTitlePositionAdjustment(UIOffset(horizontal: 30, vertical: 30), forBarMetrics: UIBarMetrics.Default)

或本地与物品出口本身:

(SWIFT)

self.myBarItem.setTitlePositionAdjustment(UIOffset(horizontal: 30, vertical: 30), forBarMetrics: UIBarMetrics.Default)

(Obj C)

[self.myBarItem setTitlePositionAdjustment:UIOffsetMake(30, 30) forBarMetrics: UIBarMetricsDefault];

这是外观错误吗?

答案 1 :(得分:1)

我也对此感到沮丧所以我将UIBarButtonItem子类化为按预期工作。从this回答我得出了这个:

@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));
}

不可否认,它并没有考虑到不同的条形指标,但对于默认指标,我已经得到了相当好的工作。

答案 2 :(得分:1)

Change the baselineOffset of the text.

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)

A negative baselineOffset will shift the title text down, a positive value will shift it up. 0 is the default value.

I tested this on iOS 11.

答案 3 :(得分: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)

在我的情况下,背景是透明的,所以调整并没有真正有所作为。我还试图将标题移开。我认为这是de调整有用的唯一情况。

无论如何,只是一种解决方法,但我希望它有所帮助。

这里的总菜鸟,反馈意见。

答案 4 :(得分:0)

建议:在UIButton内使用UIBarButtonItem

我有一个工具栏,该工具栏的两侧都有两个较高的按钮(屏幕截图中的“向上”按钮和“向下”按钮),当用户启动操作时,我在工具栏中显示了两个按钮,上面只有文字。当我最初将它们作为UIBarButtonItem对象放置在工具栏上时,它们在工具栏上的位置非常低,而我尝试过的任何尝试都不会使它们坐得更高。它们的位置使用户很难在iPadPro设备上按下它们,因为设备底部的条形有点阻塞它们。

我发现,如果将UIButton拖动到工具栏,则IB会自动将它们放在UIBarButtonItem内作为视图,并且它们在工具栏中垂直居中。然后,我可以像往常一样调整UIButton的内容插图,并且更改可以正确反映。 (因为我只是想使它们垂直居中,所以仅使用UIButtons对我有用)。请记住,您连接的所有IBAction应该都连接到UIButton。

我使用XCode 10.1和Swift 4做到了这一点。

Storyboard outline view