【问题标题】:UINavigationItem multiple line prompt textUINavigationItem 多行提示文字
【发布时间】:2012-06-28 07:49:10
【问题描述】:

任何机构都可以给我解决方案,以 2 行显示 UINavigationItem 提示文本吗?

【问题讨论】:

    标签: uinavigationcontroller prompt uinavigationitem


    【解决方案1】:

    没有内置的方法可以做到这一点。下面是我从stackOverflow 帖子UINavigationItem with prompt and activity indicator 整理出来的一个似乎效果很好的解决方法

    这是它创建的模拟器屏幕截图:

    请注意,由于文本是 UILabel,因此您也可以修改其颜色、字体或其他任何内容。

    // I have this code in viewDidLoad
    UIView                      *viewContainingPrompt;
    UIBarButtonItem             *promptButtonItem;
    
    // Configuring the prompt title of the navigation bar so it is present but empty
    [self.navigationItem setPrompt: @""];
    
    // We will create a UIBarButtonItem that has a custom view (viewContainingPrompt).
    // A subview of viewContainingPrompt will be a UILabel (headerLabel)
    // We need to have this "intermediate" view to position the label at the right position 
    // (the UIBarButtonItem ignores the origin and height of its custom view)
    viewContainingPrompt = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 0, 85)];
    viewContainingPrompt.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    
    // Choose a width that puts 10 points on either end...
    CGFloat labelWidth = self.navigationController.navigationBar.bounds.size.width - 20.0;
    // Note that the '-60' below is determined by the width of the back button
    // If someone can figure out how to determine this width at runtime this code
    // would be much more robust.
    UILabel *headerLabel = [[UILabel alloc] initWithFrame: CGRectMake(-60,-8,labelWidth,36)];
    headerLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    headerLabel.text = @"A quite long prompt string that will wrap to a second line to demonstrate multiline prompt.";
    headerLabel.font = [UIFont systemFontOfSize: 14];
    headerLabel.numberOfLines = 0; // Zero gives as many lines as will fit, could be 2
    headerLabel.backgroundColor = [UIColor clearColor];
    headerLabel.textColor = [UIColor colorWithRed: .1 green: .1 blue: .2 alpha: 0.8f];
    headerLabel.shadowColor = [UIColor colorWithRed: 1 green: 1 blue: 1 alpha: 0.5f];
    headerLabel.shadowOffset = CGSizeMake( 0, 1 );
    headerLabel.textAlignment = UITextAlignmentCenter;
    [viewContainingPrompt addSubview: headerLabel];
    //[headerLabel release]; // Uncomment if not using ARC
    
    promptButtonItem = [[UIBarButtonItem alloc] initWithCustomView: viewContainingPrompt];
    self.navigationItem.leftBarButtonItem = promptButtonItem;
    self.navigationItem.leftItemsSupplementBackButton = YES;
    //[viewContainingPrompt release]; // Uncomment if not using ARC
    //[promptButtonItem release]; // Uncomment if not using ARC
    

    如果有人就如何在执行过程中计算后退按钮的宽度提供反馈,我将不胜感激,这样就不必对宽度进行硬编码。

    我不认为其中包含任何私有 API 或其他非法代码。

    【讨论】:

      猜你喜欢
      • 2014-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多