1

UINavigationBar があり、leftBarButtonItem と rightBarButtonItem が設定されています。左バーは次のように設定されます。

UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [backButton setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
        UIBarButtonItem *myButton = [[[UIBarButtonItem alloc] initWithCustomView:backButton] autorelease];
        self.navigationItem.leftBarButtonItem = myButton;

rightbarbutton は次のように設定されます。

UIButton *doneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
 UIImage *doneButtonImage = nil;
        doneButtonImage = [UIImage imageNamed:@"done.png"];
        [doneBtn setImage:doneButtonImage forState:UIControlStateNormal];
  doneBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:doneBtn];
    self.navigationItem.rightBarButtonItem = doneBarButtonItem;

back の幅は 23px で、done は 72px で、等しくありません。問題は、常に中央に配置したいラベルがあることです..ラベルのテキストが長すぎて右のバーボタンに干渉する場合は、テキストをクリップしたい. 設定方法は次のとおりです。

titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    titleLabel.text = self.pageTitle; // Can't change
    titleLabel.adjustsFontSizeToFitWidth = YES;
    titleLabel.clipsToBounds = YES;
    titleLabel.numberOfLines = 1;
    titleLabel.font = [UIFont fontWithName:@"PTSans-Narrow" size:30.0]; 
    titleLabel.textColor = [UIColor colorWithWhite:0.6 alpha:1.0];
    titleLabel.backgroundColor = [UIColor yellowColor];
    titleLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    titleLabel.textAlignment = UITextAlignmentCenter;
    [titleLabel sizeToFit];
    titleLabel.frameHeight = self.navigationController.navigationBar.frameHeight;


 self.navigationItem.titleView = titleLabel;

ただし、これはタイトルを中央に配置しません..どうすればできますか?

4

5 に答える 5

1

最初に self.pageTitle の幅を数え、それに応じて titleLabel.I の位置を設定することをお勧めします:-

titleLabel = [[UILabel alloc] init];
titleLabel.text = self.pageTitle; // Can't change

**CGSize maximumTitleLabelSize = CGSizeMake(200,40);//Set maximum width that an self.pageTitle can have.
CGSize expectedLabelSize = [titleLabel.text titleLabel.font 
                                            constrainedToSize:maximumTitleLabelSize
                                                lineBreakMode:UILineBreakModeWordWrap];
//adjust the label the the new width.
CGRect newFrame = titleLabel.frame;
newFrame.size.width = expectedLabelSize.width;
titleLabel.size.width = newFrame.size.width;**    
if(titleLabel.size.width==100)
{
titleLabel.frame = cgRectMake(85,5,titleLabel.size.width,30);
}
else if(titleLabel.size.width==200)
{
titleLabel.frame = cgRectMake(36,5,titleLabel.size.width,30);
}

titleLabel.adjustsFontSizeToFitWidth = YES;
titleLabel.clipsToBounds = YES;
titleLabel.numberOfLines = 1;
titleLabel.font = [UIFont fontWithName:@"PTSans-Narrow" size:30.0]; 
titleLabel.textColor = [UIColor colorWithWhite:0.6 alpha:1.0];
titleLabel.backgroundColor = [UIColor yellowColor];
titleLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight;
titleLabel.textAlignment = UITextAlignmentCenter;
[titleLabel sizeToFit];
titleLabel.frameHeight = self.navigationController.navigationBar.frameHeight;

self.navigationItem.titleView = titleLabel;

これを試してみてください.それはあなたを助けます.Thanks :)

于 2012-04-19T06:52:55.877 に答える
1

これは最も簡単な解決策です。sizeToFitメソッドを に追加する前に使用しtitleViewます。

UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.text = @"My Title";
titleLabel.font = [UIFont boldSystemFontOfSize:17.0f];
titleLabel.textColor = [UIColor whiteColor];
[titleLabel sizeToFit];

self.navigationItem.titleView = titleLabel;
于 2014-06-06T04:50:47.400 に答える
0

NavigationItem を使用して左右のバー ボタンを追加しないでください。代わりに addsubview メソッドを使用して、ナビゲーション バーのカスタム ボタンを追加してください。

于 2012-04-19T06:50:14.507 に答える
0

ツールバーを使用して、ナビゲーション バーに複数のボタンを追加します。

   // create a toolbar to have two buttons in the right
UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 133, 44.01)];

// create the array to hold the buttons, which then gets added to the toolbar
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];

// create a standard "add" button
UIBarButtonItem* bi = [[UIBarButtonItem alloc]`enter code here`
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:NULL];
bi.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi];
[bi release];

// create a spacer
bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[buttons addObject:bi];
[bi release];

// create a standard "refresh" button
bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refresh:)];
bi.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi];
[bi release];

// stick the buttons in the toolbar
[tools setItems:buttons animated:NO];

[buttons release];

// and put the toolbar in the nav bar
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];
[tools release];
于 2012-04-19T06:51:59.133 に答える
0

このコードを使用するだけで問題なく動作します

UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 5, 190, 30)];
titleLabel.textColor = [UIColor whiteColor];
titleLabel.textAlignment = UITextAlignmentCenter;
titleLabel.text =@"My Wish List"; 
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.font = [UIFont fontWithName:@"Copperplate" size:16.0];
titleLabel.minimumFontSize = 10;
titleLabel.autoresizingMask=UIViewAutoresizingFlexibleWidth;
self.navigationItem.titleView = titleLabel;
[titleLabel release];

私は自分のプロジェクトでもこのコードを使用しています.......:-)

于 2012-04-21T14:27:28.947 に答える