简体   繁体   中英

Changing the position of navigationItem titleView to left side

I need something like "< Delivery" an arrow which is a back button with a label at its side to left but instead of this I'm getting something like "< Delivery"

I have searched a lot but didn't get anything useful that worked. let me show you the code up till now.

// 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];

please help and thanks in advance.

Why cant you use like as follows?

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;
//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];

Previous Explanation

You need to use [UIBarButton alloc] initWithCustomView: where you pass your own UILabel as custom View

and then set

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

That way you can have label and image on the left side of the navigation bar

Please try the below method and let me know the result.

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;

Thanks!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM