【问题标题】:Navigation Bar Back Button, text not center alligined导航栏后退按钮,文本不居中对齐
【发布时间】:2012-06-08 07:43:42
【问题描述】:

我正在使用外观代理自定义导航栏中的后退按钮,同时设置图像和文本属性。一切正常,但后退按钮中的文本未居中对齐。

这是我的代码。

UIBarButtonItem *backButton = [[[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStyleBordered target:nil action:nil] autorelease];
 UIImage *buttonBack32 = [[UIImage imageNamed:@"NavigationBackButton"] 
                         resizableImageWithCapInsets:UIEdgeInsetsMake(0, 10, 0, 5)];

[[UIBarButtonItem appearance] setBackButtonBackgroundImage:buttonBack32 forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
 [[UIBarButtonItem appearance] setTitleTextAttributes:
 [NSDictionary dictionaryWithObjectsAndKeys:
        [UIColor whiteColor], 
        UITextAttributeTextColor, 
        [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8], 
        UITextAttributeTextShadowColor, 
        [NSValue valueWithUIOffset:UIOffsetMake(0, -1)], 
        UITextAttributeTextShadowOffset,
  [UIFont fontWithName:@"xxx" size:16.0], 
  UITextAttributeFont,
  nil] 
                                            forState:UIControlStateNormal]; 
self.navigationItem.backBarButtonItem = backButton;

如果我将字体大小设置为“0.0”,那么文本会变得太小并且仍然没有居中对齐。

提前感谢您的帮助。

【问题讨论】:

  • 你是指垂直对齐还是水平对齐?
  • 我认为尝试这可能没有帮助,但我在 textview 和 lable 中做这件事,但不是在 BarButton 中,所以我会尝试然后发布答案 [[UIBarButtonItem appearance] setTextAlignment:UITextAlignmentCenter]; :)
  • ParasJoshi:它不起作用。
  • 我在谈论垂直对齐

标签: iphone cocoa-touch ipad ios5 uinavigationbar


【解决方案1】:

您可以使用 setTitlePositionAdjustment:forBarMetrics: 方法根据需要设置标题偏移量。例如:

[[UIBarButtonItem appearance] setTitlePositionAdjustment:UIOffsetMake(0.0f, 5.0f) forBarMetrics:UIBarMetricsDefault];

【讨论】:

  • 为我工作(y)
【解决方案2】:

要向上调整文本,我必须使用这个:

    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -1.5) forBarMetrics:UIBarMetricsDefault];

【讨论】:

    【解决方案3】:
    You can use this :
    
    UIView* leftContainer = [[UIView alloc] initWithFrame:CGRectMake(0.0, 4.0, 70.0, 44.0)];
    UIImage *profileImg = [UIImage imageNamed:@"account-btn.png"];
    UIButton *profilebutton = [UIButton buttonWithType:UIButtonTypeCustom];
    profilebutton.frame = CGRectMake(0.0, 7.0, 70.0, 30.0 );
    [profilebutton setImage:profileImg forState:UIControlStateNormal];
    [profilebutton addTarget:self action:@selector(profileBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    [leftContainer addSubview:profilebutton];
    
    UIBarButtonItem* item = [[UIBarButtonItem alloc] initWithCustomView:leftContainer];
    self.navigationItem.leftBarButtonItem = item;
    
    [leftContainer release];
    [item release];
    

    【讨论】:

      猜你喜欢
      • 2014-03-20
      • 2016-06-12
      • 1970-01-01
      • 2018-07-30
      • 2019-02-23
      • 2017-02-19
      • 1970-01-01
      • 2020-02-21
      • 2021-11-06
      相关资源
      最近更新 更多