为什么在调用presentmodalviewcontroller时navigationItem.titleView左对齐?

发布于 2024-09-03 17:37:36 字数 1154 浏览 5 评论 0原文

我正在使用 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];
}

截图: 在此处输入图像描述

知道为什么会发生这种情况吗?谢谢。

I'm using a UILabel for the titleView of a navigation bar (I'm making simple in-app web browser). It works fine, except that when I present a modal view controller, the titleView shifts from the center of the navbar to the far left (underneath the back button). I've tested in 3.0 and up. Here is relevant code:

- (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];
}

Screenshots:
enter image description here

Any idea why this is happening? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

荭秂 2024-09-10 17:37:36

我通过一些点击和尝试研究了这个问题,发现了以下事实:

  • 如果 UINavigationBar 没有 rightBarButtonItem,则 titleView 会向右移动约 30 点。
  • 可以为 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;

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

I looked into the problem with some hit and try and found the following facts:

  • If the UINavigationBar doesn't have the rightBarButtonItem, the titleView shifts towards the right by ~30pts.
  • It could be reproduced for leftBarButtonItem. But I haven't tried.

In a scenario where the a default UINavigationBar's (with no changes to rightBarButtonItem defaults) titleView is set. And then a new UIView is pushed to the navigation stack which HAS a rightBarButtonItem. Now, if this view is popped [with back button], the navigation bar will remove the rightBarButtonItem. And this will account for the weird offset that shifts the titleView towards a side.

How I fixed the problem was like this:

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;

Everything is sweet now. yummmm.

呢古 2024-09-10 17:37:36

感谢 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];
}

Thanks to DougW for pointing me in right direction. Here's the best hack I found. Basically I retain the UILabel as a class property. Before presenting modal view I unset the titleView, and then reset it immediately after. When the modal view is dismissed I unset then reset the titleView. To the user none of this is visibly notable.

-(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];
}
醉酒的小男人 2024-09-10 17:37:36

它有动画吗?它可能会对标题视图进行动画处理,就像它正在转换到新视图一样。我没有看到你写的代码有什么问题。

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

坦率地说,整个 UINavigation 系统很糟糕。由于像这样的奇怪问题,我们继续重写它。

Does it animate? It may be animating the title view as though it's transitioning to a new view. I don't see anything wrong with your code as written.

I would suggest in your displayComposerSheet, you just unset the titleView, or animate the alpha of the titleView to 0.0. Then, animate it back to 1.0 when you dismiss the modal view controller. Not ideal, but it may look better that way.

Frankly, the whole UINavigation system is crap. We went ahead and re-wrote it ground up because of bizarre issues like these.

谎言月老 2024-09-10 17:37:36

唯一的问题是你的框架尺寸。所以你必须改变它。

试试这个。

   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;

The only problem is your frame size. so u have to change it.

Try this one.

   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;
近箐 2024-09-10 17:37:36

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

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

它对我有用。

You can try move the code in viewDidAppear:

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

It works for me.

执着的年纪 2024-09-10 17:37:36

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

If you change the width size to be small like 100 points or smaller instead of 120 you set, this problem may go away. Setting width of the label smaller worked for me.

三生一梦 2024-09-10 17:37:36
  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;
  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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文