【问题标题】:Why does navigationItem.titleView align left when presentmodalviewcontroller called?为什么调用presentmodalviewcontroller时navigationItem.titleView左对齐?
【发布时间】:2010-06-03 17:53:53
【问题描述】:

我正在使用 UILabel 作为导航栏的 titleView(我正在制作简单的应用内网络浏览器)。它工作正常,除了当我展示一个模态视图控制器时,titleView 从导航栏的中心移动到最左边(在后退按钮下方)。我已经在 3.0 及更高版本中进行了测试。以下是相关代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Title view label
    CGRect labelFrame = CGRectMake(0.0, 0.0, 120.0, 36.0); 
    UILabel *label = [[[UILabel alloc] initWithFrame:labelFrame] autorelease];
    label.font = [UIFont boldSystemFontOfSize:14];
    label.numberOfLines = 2;
    label.backgroundColor = [UIColor clearColor];
    label.textAlignment = UITextAlignmentCenter;
    label.textColor = [UIColor whiteColor];
    label.shadowColor = [UIColor blackColor];
    label.shadowOffset = CGSizeMake(0.0, -1.0);
    label.lineBreakMode = UILineBreakModeMiddleTruncation;
    self.navigationItem.titleView = label;
}

-(void)displayComposerSheet:(NSString*)mailto 
{
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self;
    [self presentModalViewController:picker animated:YES];
    [picker release];
}

截图:

知道为什么会这样吗?谢谢。

【问题讨论】:

    标签: ios iphone cocoa-touch uinavigationbar


    【解决方案1】:

    我调查了一些问题并尝试发现以下事实:

    • 如果 UINavigationBar 没有 rightBarButtonItem,则 titleView 向右移动 ~30pts。
    • 可以为 leftBarButtonItem 复制它。但我没试过。

    在设置了默认 UINavigationBar(不更改 rightBarButtonItem 默认值)titleView 的场景中。然后一个新的 UIView 被推送到具有 rightBarButtonItem 的导航堆栈。现在,如果这个视图被弹出[带有后退按钮],导航栏将删除 rightBarButtonItem。这将解释将 titleView 向一侧移动的奇怪偏移量。

    我是如何解决这个问题的:

    self.navigationItem.titleView = myCustomTitleView;
    
    // Fake right button to align titleView properly.
    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:[[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 1)]];
    // Width equivalent to system default Done button's (which appears on pushed view in my case).
    rightBarButtonItem.enabled = NO;
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
    

    现在一切都很甜蜜。嗯嗯。

    【讨论】:

      【解决方案2】:

      感谢 DougW 为我指明了正确的方向。这是我发现的最好的黑客。基本上我将 UILabel 保留为类属性。在呈现模态视图之前,我取消了 titleView,然后立即将其重置。当模式视图被关闭时,我取消设置然后重置 titleView。对于用户来说,这一切都不是显而易见的。

      -(void)displayComposerSheet:(NSString*)mailto 
      {
          self.navigationItem.titleView = nil;
          MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
          picker.mailComposeDelegate = self;
          picker.navigationBar.tintColor = [APPDELEGATE getNavTintColor];
          [picker setToRecipients:[NSArray arrayWithObject:mailto]];
          [self presentModalViewController:picker animated:YES];
          [picker release];
          self.navigationItem.titleView = titlelabel;
      }
      
      - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 
      {   
          self.navigationItem.titleView = nil;
          self.navigationItem.titleView = titlelabel;
          [self dismissModalViewControllerAnimated:YES];
      }
      

      【讨论】:

      • 在 - (void)loadView 我添加了 self.label = [[UILabel alloc] initWithFrame:self.navigationController.navigationBar.frame]; self.navigationItem.titleView = self.label;然后只更改了 self.label 的文本
      【解决方案3】:

      它有动画吗?它可能正在为标题视图设置动画,就好像它正在转换到新视图一样。我看不出你写的代码有什么问题。

      我建议在您的 displayComposerSheet 中,您只需取消设置 titleView,或将 titleView 的 alpha 设置为 0.0。然后,当您关闭模态视图控制器时,将其设置回 1.0。不理想,但这样看起来可能会更好。

      坦率地说,整个 UINavigation 系统都是垃圾。由于这些奇怪的问题,我们继续并重新编写了它。

      【讨论】:

      • 是的,模态视图是动画的(第二个屏幕截图显示了正在进行的模态视图的演示)。问题是即使在模态视图被关闭后,标题视图仍然是左对齐的。
      • 感谢您的建议。我尝试了一些变体,发现了一个“解决”问题的技巧。代码在我的答案中。我会支持你,但我没有足够的声誉。
      • 很高兴我能帮上忙。是的,UINav 的东西是一个严重的问题。很遗憾,我们不得不求助于这样的 hacky 修复。
      【解决方案4】:

      唯一的问题是您的帧大小。所以你必须改变它。

      试试这个。

         UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 36.0)];
      
         label.font = [UIFont boldSystemFontOfSize:14];
         label.numberOfLines = 2;
         label.backgroundColor = [UIColor clearColor];
         label.textAlignment = UITextAlignmentCenter;
         label.textColor = [UIColor whiteColor];
         label.shadowColor = [UIColor blackColor];
         label.shadowOffset = CGSizeMake(0.0, -1.0);
         label.lineBreakMode = UILineBreakModeMiddleTruncation;
         label.text=@"Stack Overflow";  
         self.navigationItem.titleView = label;
      

      【讨论】:

      • 嗯。所以我尝试将宽度更改为 320,但同样的事情发生了。它似乎忽略了 UILabel 的框架 - 而是将标签中的文本左对齐到屏幕的左边缘:-/
      【解决方案5】:

      您可以尝试移动viewDidAppear中的代码:

      - (void)viewDidAppear:(BOOL)animated {
          [super viewDidAppear:animated];
          // You code to customize title view
          self.navigationItem.titleView = logoImage;
      }
      

      它对我有用。

      【讨论】:

        【解决方案6】:

        如果您将宽度大小更改为 100 磅或更小,而不是您设置的 120,则此问题可能会消失。将标签的宽度设置得更小对我有用。

        【讨论】:

          【解决方案7】:
            UIView *view=    [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
            [view setUserInteractionEnabled:NO];
            view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"logo_small.png"]];
            UIBarButtonItem *barButton=[[UIBarButtonItem alloc]initWithCustomView:view ];
            self.navigationItem.leftBarButtonItem = barButton;
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-10-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-05-06
            • 2019-08-21
            相关资源
            最近更新 更多