I have the code likes, in the title bar I am having 3 buttons, left search button, right camera button and option button, in between I am having title.
What I want to do is, I want to display the title centered between the search button and Camera button, can anyone help me how to do this.
//Left Button
UIBarButtonItem *leftSearch = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"btn_search"] style:UIBarButtonItemStyleBordered target:self action:#selector(gotoSearch)];
leftSearch.imageInsets = UIEdgeInsetsMake(0, -5.0f, 0, 0);
[leftSearch setTintColor:[UIColor whiteColor]];
/////////
//Right Button -- First Camera
UIBarButtonItem *rightCamera = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:#"icon_camera"]
style:UIBarButtonItemStyleBordered
target:self
action:#selector(gotoPhotoGallery)];
[rightCamera setTintColor:[UIColor whiteColor]];
rightCamera.imageInsets = UIEdgeInsetsMake(0, 0, 0, -59.0f);
///////////////////
//Right second Button -- for options menu
UIBarButtonItem *rightMenu = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"header_option"] style:UIBarButtonItemStyleBordered target:self action:#selector(gotoPhotoGallery)];
[rightMenu setTintColor:[UIColor whiteColor]];
rightMenu.imageInsets = UIEdgeInsetsMake(0, 0, 0, 5.0f);
///////////////////
NSArray *buttons = #[rightMenu, rightCamera];
self.navigationItem.rightBarButtonItems = buttons;
[[self navigationItem] setLeftBarButtonItem:leftSearch];
//Title Label
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont fontWithName:#"Museo Sans" size:20];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.text = LoginUserName;
self.navigationItem.titleView = label;
[label sizeToFit];
Create your title label with the same width and height as the navigation bar. For example
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width,44)];
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.text = LoginUserName;
titleLabel.textColor = [UIColor whiteColor];
[self.navigationController.navigationBar addSubview:titleLabel];
This will put your text in the middle.
Related
I am trying to set the next/previous buttons on my keyboard toolbar, I want get put in navigation to go next/Previous textfield. Here is what I am trying.
How can I use the system bar button item to change to textfield?
-(void)addTeam{
UIView * view = [[UIView alloc]initWithFrame:CGRectMake(15, (0+(50*[teamsArray count])), 273, 50)];
[view setTag:[teamsArray count]+1];
UILabel *lblTeamName = [[UILabel alloc ] initWithFrame:CGRectMake(5,0,58,20)];
[lblTeamName setBackgroundColor:[UIColor clearColor]];
lblTeamName.font = [UIFont fontWithName:#"Arial Rounded MT Bold" size:(12.0)];
lblTeamName.text = [NSString stringWithFormat:#"Team %i",[teamsArray count]+1];
[lblTeamName setTag:7];
[view addSubview:lblTeamName];
UITextField* txtTeamName = [[UITextField alloc]initWithFrame:CGRectMake(5,20,150,30)];
[txtTeamName setBorderStyle:UITextBorderStyleRoundedRect];
[txtTeamName setPlaceholder:#"Team Name"];
[txtTeamName addTarget:self action:#selector(hideKeyboard) forControlEvents:UIControlEventEditingDidEndOnExit];
[txtTeamName setDelegate:self];
[view addSubview:txtTeamName];
UIButton *btnDelete = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnDelete addTarget:self
action:#selector(btnDelete_OnTouch:)
forControlEvents:UIControlEventTouchDown];
[btnDelete setTag:[teamsArray count]];
[btnDelete setTitle:#"-" forState:UIControlStateNormal];
btnDelete.frame = CGRectMake(160, 20, 20, 30);
[view addSubview:btnDelete];
[scrollView addSubview:view];
[teamsArray addObject:view];
}
-(BOOL)textFieldShouldBeginEditing: (UITextField *)textField
{
UIToolbar * keyboardToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
keyboardToolBar.barStyle = UIBarStyleDefault;
[keyboardToolBar setItems: [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithTitle:#"Previous" style:UIBarButtonItemStyleBordered target:self action:#selector(previousTextField)],
[[UIBarButtonItem alloc] initWithTitle:#"Next" style:UIBarButtonItemStyleBordered target:self action:#selector(nextTextField)],
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(resignKeyboard)],
nil]];
textField.inputAccessoryView = keyboardToolBar;
}
- (void)nextTextField {
????
}
-(void)previousTextField
{
????
}
you could use next and previous button,
How to navigate through textfields (Next / Done Buttons)
And do this in ViewDidLoad
UIToolbar * keyboardToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
keyboardToolBar.barStyle = UIBarStyleDefault;
[keyboardToolBar setItems: [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithTitle:#"Previous" style:UIBarButtonItemStyleBordered target:self action:#selector(previousTextField)],
[[UIBarButtonItem alloc] initWithTitle:#"Next" style:UIBarButtonItemStyleBordered target:self action:#selector(nextTextField)],
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(resignKeyboard)],
nil]];
textField.inputAccessoryView = keyboardToolBar;
AppDelegate.m Navbar Customization:
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque];
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"navbar"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setTitleTextAttributes: #{
UITextAttributeTextColor: [UIColor colorWithRed:(56/255.0) green:(61/255.0) blue:(63/255.0) alpha:1],
UITextAttributeTextShadowColor: [UIColor grayColor],
UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0.0f, 1.0f)],
UITextAttributeFont: [UIFont fontWithName:#"ProximaNova-Bold" size:15.0f]
}];
LViewController.m Navbar Custom Left/Right Buttons:
// Left Button
UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
[leftButton setImage:[UIImage imageNamed:#"menu"] forState:UIControlStateNormal];
[leftButton addTarget:self action:#selector(openLeftMenu) forControlEvents:UIControlEventTouchUpInside];
[leftButton setFrame:CGRectMake(0, 0, 45, 44)];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
// Right Button
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
[rightButton setImage:[UIImage imageNamed:#"location"] forState:UIControlStateNormal];
[rightButton addTarget:self action:#selector(openRightMenu) forControlEvents:UIControlEventTouchUpInside];
[rightButton setFrame:CGRectMake(0, 0, 45, 44)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
Result (Title not Centered):
I tried a lot of tips, but anyone worked:
Working with custom fonts on iOS is fraught with peril when it comes to vertical orientation of the text. Your best bet here is to embed the title in a UILabel, then place that UILabel in a UIView, positioned appropriately. Something like this:
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.text = #"Test";
UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0.0f,
0.0f,
320.0f,
44.0f)];
titleLabel.frame = …; // Compute the frame you need based on the chosen font, set
// appropriately.
[container addSubview:titleLabel];
self.navigationItem.titleView = container;
I have my navigation bar title as a label that I am trying to resize the font. I have the right bar button set up as an array of two buttons. Instead of resizing the title just knocks off one of the right buttons.
here is my code
- (void)setTitle:(NSString *)title
{
[super setTitle:title];
UILabel *titleView = (UILabel *)self.navigationItem.titleView;
if (!titleView) {
titleView = [[UILabel alloc] initWithFrame:CGRectZero];
titleView.backgroundColor = [UIColor clearColor];
titleView.font = [UIFont boldSystemFontOfSize:18.0];
titleView.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
titleView.textColor = [UIColor greenColor]; // Change to desired color
[titleView sizeToFit];
titleView.minimumScaleFactor = .33f;
CGRect frame = self.navigationItem.titleView.frame;
frame.origin .x = 10;
self.navigationItem.titleView = titleView;
self.navigationItem.titleView.frame = frame;
self.navigationItem.titleView = titleView;
}
titleView.text = title;
[titleView sizeToFit];
titleView.adjustsFontSizeToFitWidth = TRUE;
}
as you can see I have set the max font size to 18 and the min to 6 being .33 of the max size. I have also tried to align it to the left but that didn't work. The following code set up an array of two button on the right side.
buttonimage = [UIImage imageNamed:#"share icon1 Camera.png"];
UIBarButtonItem *saveButton =[[UIBarButtonItem alloc]
initWithImage:buttonimage
style:UIBarButtonItemStyleBordered
target:self
action:#selector(shareByActivity:)];
buttonimage2 = [UIImage imageNamed:#"mail icon sm Sound.png"];
UIBarButtonItem *soundButton =[[UIBarButtonItem alloc]
initWithImage:buttonimage2
style:UIBarButtonItemStyleBordered
target:self
action:#selector(shareByActivity2:)];
self.navigationItem.rightBarButtonItem = saveButton;
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:soundButton,saveButton, nil];
how can I get the title to resize and not cover the second button of the array"
Can I set an if statement that if the title string is more that so many characters then the font size is something.
I use a self.navigationItem.titleView and I set two UIButtons as the self.navigationItem.rightBarButtonItems in the UINavigationBar. I have achieved this in my navigationBar like so
Whenever I rotate the view controller landscape and then back, the uibarbutton item gets cut off like so
The code I use to set the titleView is this:
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 44)];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.numberOfLines = 2;
titleLabel.font = [UIFont boldSystemFontOfSize:13.0f];
titleLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
titleLabel.textAlignment = UITextAlignmentCenter;
titleLabel.textColor = [UIColor whiteColor];
titleLabel.text = self.tabTitle;
self.navigationItem.titleView = titleLabel;
I have no idea why this is happening and I wonder if if any of you know how to fix this.
I have the following function which displays the top bar on my app. It consists of an image and a UIBarButton. I call it from viewWillAppear. When I push the current controller and move to the next controller and then move Back to the main controller, the right bar button disappears. Please help.
-(void)setTopBar
{
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.6367 green:0.6367 blue:0.6367 alpha:1.0];
UIImage *titleImage = [UIImage imageNamed: MAIN_TOPBAR];
if([self.navigationController.navigationBar respondsToSelector:#selector(setBackgroundImage:forBarMetrics:)] ) {
//iOS 5 new UINavigationBar custom background
[self.navigationController.navigationBar setBackgroundImage:titleImage forBarMetrics: UIBarMetricsDefault];
// NSLog(#"loading nav items");
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"More Apps" style:UIBarButtonItemStylePlain target:self action:#selector(loadFAADAds)];
rightButton.tag = 11111;
self.navigationItem.rightBarButtonItem = rightButton;
[rightButton release];
}
else
{
CGRect titleFrame = self.navigationController.navigationBar.frame;
UIImageView *titleImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, titleFrame.size.width,titleFrame.size.height)];
[titleImageView setImage:titleImage];
[titleImageView setTag:TOPBAR_TAG];
// [self.navigationController.navigationBar sendSubviewToBack:titleImageView];
// self.navigationItem.titleView = titleImageView;
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"More Apps" style:UIBarButtonItemStylePlain target:self action:#selector(loadFAADAds)];
self.navigationItem.rightBarButtonItem = rightButton;
//self.navigationController.navigationItem.rightBarButtonItem = rightButton;
[self.navigationController.navigationBar insertSubview:titleImageView atIndex:0];
[self.navigationController.navigationBar sendSubviewToBack:self.navigationController.navigationBar];
[rightButton release];
}
[titleImage release];
}
-(void)setTopBar
{
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.6367 green:0.6367 blue:0.6367 alpha:1.0];
UIImage *titleImage = [UIImage imageNamed: MAIN_TOPBAR];
if([self.navigationController.navigationBar respondsToSelector:#selector(setBackgroundImage:forBarMetrics:)] ) {
//iOS 5 new UINavigationBar custom background
[self.navigationController.navigationBar setBackgroundImage:titleImage forBarMetrics: UIBarMetricsDefault];
// NSLog(#"loading nav items");
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"More Apps" style:UIBarButtonItemStylePlain target:self action:#selector(loadFAADAds)];
rightButton.tag = 11111;
self.navigationItem.rightBarButtonItem = rightButton;
// [rightButton release];
}
else
{
CGRect titleFrame = self.navigationController.navigationBar.frame;
UIImageView *titleImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, titleFrame.size.width,titleFrame.size.height)];
[titleImageView setImage:titleImage];
[titleImageView setTag:TOPBAR_TAG];
// [self.navigationController.navigationBar sendSubviewToBack:titleImageView];
// self.navigationItem.titleView = titleImageView;
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"More Apps" style:UIBarButtonItemStylePlain target:self action:#selector(loadFAADAds)];
self.navigationItem.rightBarButtonItem = rightButton;
//self.navigationController.navigationItem.rightBarButtonItem = rightButton;
[self.navigationController.navigationBar insertSubview:titleImageView atIndex:0];
[self.navigationController.navigationBar sendSubviewToBack:self.navigationController.navigationBar];
//[rightButton release];
}
// [titleImage release];
}