将navigationItem titleView的位置更改为左侧

时间:2014-03-15 11:24:04

标签: ios iphone

我需要一些像“< Delivery”这样的箭头,这是一个后面的按钮,左侧有一个标签,但不是这样我得到的东西就像“< Delivery”

我经常搜索但没有得到任何有用的东西。让我告诉你到目前为止的代码。

// setting up background image of backbutton
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *backBtnImage = [UIImage imageNamed:@"Gallery--arrow.png"]  ;
[backBtn setBackgroundImage:backBtnImage forState:UIControlStateNormal];
[backBtn addTarget:self action:@selector(goback) forControlEvents:UIControlEventTouchUpInside];
backBtn.frame = CGRectMake(0, 0,8,15);
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:backBtn] ;
self.navigationItem.leftBarButtonItem = backButton;
// ends 


// text beside back button in header
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,150,30)];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont fontWithName:@"GoodMobiPro-CondBold" size:24];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
label.textAlignment = NSTextAlignmentLeft;
label.textColor = [UIColor whiteColor];
self.navigationItem.titleView = label;
label.text = selectedArea;
[label sizeToFit];

请提前帮助和谢谢。

3 个答案:

答案 0 :(得分:2)

为什么你不能使用如下?

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,150,30)];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont fontWithName:@"GoodMobiPro-CondBold" size:24];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
label.textAlignment = NSTextAlignmentLeft;
label.textColor = [UIColor whiteColor];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:label] ;
self.navigationItem.leftBarButtonItem = backButton;

答案 1 :(得分:0)

//back button first
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *backBtnImage = [UIImage imageNamed:@"Gallery--arrow.png"]  ;
[backBtn setBackgroundImage:backBtnImage forState:UIControlStateNormal];
[backBtn addTarget:self action:@selector(goback) forControlEvents:UIControlEventTouchUpInside];
backBtn.frame = CGRectMake(0, 0,8,15);
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:backBtn] ;

//then label 

UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.text = @"<";
// other properties , fonts , background etc
[label sizeToFit];

UIBarButtonItem *leftLabelBarButton = [[UIBarButtonItem alloc] initWithCustomView:label target:yourTarget selector:yourSelector];

Now set self.navigationItem.leftBarButtonItems = @[backButton,leftLabelBarButton];
  

上一个解释

您需要使用[UIBarButton alloc] initWithCustomView:将自己的UILabel作为自定义视图传递

然后设置

self.navigationItem.leftBarButtonItems = @[customViewBarButton,imageBarButton];

这样,您可以在导航栏的左侧显示标签和图像

答案 2 :(得分:0)

请尝试以下方法,让我知道结果。

UIView * customView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 30.0)];

customView.backgroundColor = [UIColor clearColor];

UIButton * customButton = [UIButton buttonWithType:UIButtonTypeCustom];

[customButton setBackgroundImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];

customButton.frame = CGRectMake(0, 0, 100.0, 30.0);

[customButton addTarget:self action:@selector(backSelected:) forControlEvents:UIControlEventTouchUpInside];

[customView addSubview:customButton];

UILabel * customLabel = [[UILabel alloc] initWithFrame:CGRectMake(customButton.frame.origin.x + customButton.frame.size.width, customButton.frame.origin.y, 200.0, 30.0)];

customLabel.backgroundColor = [UIColor clearColor];

customLabel.text = @"Your title";

[customView addSubview:customLabel];

UIBarButtonItem * leftItem = [[UIBarButtonItem alloc] initWithCustomView:customView];

self.navigationItem.leftBarButtonItem = leftItem;

leftItem = nil;

谢谢!