iOS UIBarButtonItem 对齐方式

Posted

技术标签:

【中文标题】iOS UIBarButtonItem 对齐方式【英文标题】:iOS UIBarButtonItem alignments 【发布时间】:2013-10-10 10:32:25 【问题描述】:

在我的应用中,我添加了两个带有导航栏的 UIBarButtonItem。在 ios6 及更低版本中它看起来像这样

但是当我在 iOS7 中运行我的应用程序时,它看起来像

问题是:刷新按钮按下。所以请帮我显示刷新按钮等于“今天”按钮。下面是我正在使用的代码,

 UIBarButtonItem* todayButton = [[UIBarButtonItem alloc] initWithTitle:@"Today"
                                                                style:UIBarButtonItemStyleBordered 
                                                               target:self
                                                               action:@selector(showTodayAction:)];
UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
                                                                              target:self
                                                                              action:@selector(refreshAction:)];
refreshButton.style = UIBarButtonItemStyleBordered;

    UIBarButtonItem *negativeSeperator = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                                                                       target:nil
                                                                                       action:nil];
    negativeSeperator.width = -12;

    TransparentToolbar *toolbar = [[TransparentToolbar alloc] initWithFrame:CGRectMake(0, -5, 100, TOOLBAR_HEIGHT)];
    toolbar.items = [NSArray arrayWithObjects:
                     negativeSeperator,
                     refreshButton, todayButton,
                     negativeSeperator,
                     nil];

    UIView *toolbarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, TOOLBAR_HEIGHT)];
    [toolbarView addSubview:toolbar];

    UIBarButtonItem *toolbarItem = [[UIBarButtonItem alloc] initWithCustomView:toolbarView];

    self.navigationItem.leftBarButtonItem = toolbarItem;

【问题讨论】:

你能看到那个问题***.com/questions/18861201/… @megan 你有解决方案吗....? @Megan 你在吗? 【参考方案1】:

试试这样......对不起,我赶时间

pragma 标记我的尝试

UIBarButtonItem* todayButton = [[UIBarButtonItem alloc] initWithTitle:@"Today"
                                                                style:UIBarButtonSystemItemRefresh
                                                               target:self
                                                               action:@selector(goBack)];
UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
                                                                               target:self
                                                                               action:@selector(goBack)];
refreshButton.style = UIBarButtonItemStyleBordered;

UIBarButtonItem *negativeSeperator = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                                                                   target:nil
                                                                                   action:nil];
negativeSeperator.width = -12;

UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
toolbar.items = [NSArray arrayWithObjects:
                 
                 negativeSeperator,refreshButton, todayButton,
                 negativeSeperator,
                 nil];//
 self.navigationItem.leftBarButtonItems = toolbar.items;

【讨论】:

【参考方案2】:

我认为不需要其他额外的视图,下面的代码会做你的工作:

UIBarButtonItem* todayButton = [[UIBarButtonItem alloc] initWithTitle:@"Today"
                                                                    style:UIBarButtonItemStyleBordered
                                                                   target:self
                                                                   action:@selector(showTodayAction:)];
    UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
                                                                                   target:self
                                                                                   action:@selector(refreshAction:)];
    refreshButton.style = UIBarButtonItemStyleBordered;

    self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:refreshButton, todayButton, nil];

【讨论】:

以上是关于iOS UIBarButtonItem 对齐方式的主要内容,如果未能解决你的问题,请参考以下文章

iOS 7后退按钮箭头垂直对齐

具有自定义视图的 UIBarButtonItem 在 iOS 7 上用作左侧或右侧导航栏项目时未正确对齐

在 iOS 中将 self.navigationItem.backBarButtonItem 与设备左侧对齐

横向模式下 UIBarButtonItem 的垂直对齐太低

后退按钮左对齐IOS 9

UIToolbar UIBarButtonItem 对齐问题