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

问题描述 投票:13回答:5

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

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

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

还有这个:

self.setTitlePositionAdjustment(UIOffset(horizontal: 30, vertical: 30), forBarMetrics: UIBarMetrics.Compact)
swift uibarbuttonitem uitoolbar
5个回答
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)

或本地与物品出口本身:

(迅速)

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

(Obj C)

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

这是Appearance的错误吗?


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

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


1
投票

更改文本的baselineOffset

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上测试了这个。


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调整有用的唯一情况。

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

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


0
投票

建议:在UIButton中使用UIBarButtonItem

我有一个工具栏,左侧有两个较高的按钮(截图中的向上按钮和向下按钮),当用户启动一个动作时,我在工具栏中显示两个按钮,只有文本。当我最初将它们作为UIBarButtonItem对象放在工具栏上时,它们在工具栏上的位置非常低,我尝试的任何东西都不会让它们坐得更高。他们的位置使得用户很难在iPadPro设备上按下它们,因为设备底部的条形有点阻挡它们。

我发现如果我将UIButton拖到工具栏上,IB会自动将那些放在UIBarButtonItem中作为视图,并且它们在工具栏中垂直居中。然后,我可以像往常一样调整UIButton的内容Insets,并且正确地反映了变化。 (因为我只是想让它们垂直居中,只是使用UIButtons为我工作)。请记住,您连接的任何IBActions都应该连接到UIButtons。

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

Storyboard outline view

© www.soinside.com 2019 - 2024. All rights reserved.