2010-06-03 41 views
6

我使用UILabel作为导航栏的titleView(我正在制作简单的应用内网页浏览器)。它工作正常,除了当我呈现一个模式视图控制器时,titleView从导航栏的中心切换到最左侧(在后退按钮下)。我已经在3.0以上测试过了。下面是相关的代码:当presentmodalviewcontroller被调用时,为什么navigationItem.titleView会左对齐?

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

截图: enter image description here

任何想法,为什么发生这种情况?谢谢。

回答

3

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

In - (void)loadView我已经添加self.label = [[UILabel alloc] initWithFrame:方法self.navigationController.navigationBar.frame]; self.navigationItem.titleView = self.label;然后只改变了自我的文本。标签 – blyabtroi 2015-12-03 15:28:16

1

它有动画吗?它可能会将标题视图动画化,就像它正在转换到新视图一样。我没有看到你的代码写错了。

我会建议在您的displayComposerSheet,你只是取消设置titleView,或动画alpha的titleView为0.0。然后,当您关闭模式视图控制器时,将它重新设置为1.0。不理想,但它可能看起来更好。

坦率地说,整个UINavigation系统是废话。由于这些奇怪的问题,我们继续前进并重新编写。

+0

是的,模态视图是动画的(第二个屏幕截图显示了正在进行的模态视图的呈现)。问题在于,即使模态视图被解散,titleview仍然保持左对齐。 – 2010-06-03 20:55:41

+0

感谢您的建议。我尝试了一些变化,发现一个“解决”问题的黑客。代码在我的答案中。我喜欢你,但我没有足够的声望。 – 2010-06-03 21:31:42

+0

很高兴我能帮到你。是的,UINav的东西是一个严肃的boondoggle。我们不得不采取这样的修补措施是一种耻辱。 – DougW 2010-06-03 21:46:19

1

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

试试这个。

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; 
    [email protected]"Stack Overflow"; 
    self.navigationItem.titleView = label; 
+0

嗯。所以我尝试将宽度更改为320,但同样的事情发生。它似乎无视UILabel的框架 - 相反,它将标签中的文本与屏幕的左边缘对齐: -/ – 2010-06-03 20:51:45

10

我看着一些命中问题和尝试,并发现了以下事实:

  • 如果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; 

现在一切都甜蜜。 yummmm。

+0

这为我工作。谢谢。 – mtb 2012-11-08 18:21:52

-1
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; 
0

您可以尝试在viewDidAppear移动代码:

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

这对我的作品。

相关问题