我有一个 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;
背面的宽度是 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;
但是,这不会使标题居中。我该怎么做?