1

我需要它左对齐。还是没有办法,我需要添加自定义标签?

4

4 回答 4

1

将其添加到左侧按钮项:

UIView *mCustView = [[UIView alloc] initWithFrame:CGRectMake(0,0,200,20)];

mCustView.backgroundColor = [UIColor redColor];

UILabel *mTextLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,200,20)];

mTextLabel.backgroundColor = [UIColor blueColor];

mTextLabel.font = [UIFont systemFontOfSize:20];

mTextLabel.textColor = [UIColor blackColor];

mTextLabel.text = @"Random text tested here, so sit back and enjoy";

[mCustView addSubview:mTextLabel];

[mTextLabel release];


UIBarButtonItem *mCustomBarItem = [[UIBarButtonItem alloc] initWithCustomView:mCustView];

self.navigationItem.leftBarButtonItem = mCustomBarItem; 

[mCustView release];
于 2012-06-03T09:45:46.620 回答
0

您需要将自定义视图添加到导航栏的-titleView

self.navigationItem.titleView = someLabel;
于 2012-06-03T07:42:34.680 回答
0

我认为您可以使用UIBarButtonSystemItemFixedSpace它提供了一种在导航栏中添加固定填充的方法:

UIBarButtonItem *fixedSpaceItem = [[[UIBarButtonItem alloc]
                                    initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                    target:nil
                                    action:NULL]
                                   autorelease];
fixedSpaceItem.width = 50;
UIBarButtonItem *cancelItem = [[[UIBarButtonItem alloc]
                                initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
                                target:nil
                                action:NULL]
                               autorelease];
// vc is some view controller
vc.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:
                                        fixedSpaceItem,
                                        cancelItem,
                                        nil];

这将使“取消”按钮缩进 50 点。

于 2012-06-03T10:46:30.390 回答
0

我自己找到了解决方案:

float offset = 210.0f;

UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, offset, 44.01)];

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];

[tools release];
于 2012-06-04T06:26:17.007 回答