Customize navigation bar with title view - ios

I am trying to add a custom view in the center of a navigation bar and I am using the following code to test it:
UIView * testView = [[UIView alloc] init];
[testView setBackgroundColor:[UIColor blackColor]];
testView.frame = CGRectMake(0, 0, 100, 35);
[self.navigationController.navigationItem.titleView addSubview:testView];
I am setting this up in the viewDidLoad method of my view controller but when i run my program
nothing seems to change in my navigation bar.
Could you help me with this?

This works. Give frame at the time of initialisation
UIView *iv = [[UIView alloc] initWithFrame:CGRectMake(0,0,32,32)];
[iv setBackgroundColor:[UIColor whiteColor]];
self.navigationItem.titleView = iv;

If you want to just customize the title for one view controller you can use
UILabel *lblTitle = [[UILabel alloc] init];
lblTitle.text = #"Diga-nos o motivo";
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor colorWithRed:77.0/255.0 green:77.0/255.0 blue:77.0/255.0 alpha:1.0];
lblTitle.shadowColor = [UIColor whiteColor];
lblTitle.shadowOffset = CGSizeMake(0, 1);
lblTitle.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:18.0];
[lblTitle sizeToFit];
self.navigationItem.titleView = lblTitle;
or if you want to customize for all view controllers use
[[UINavigationBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0],
UITextAttributeTextColor,
[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8],
UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, -1)],
UITextAttributeTextShadowOffset,
[UIFont fontWithName:#"Arial-Bold" size:10.0],
UITextAttributeFont,
nil]];

Replace
[self.navigationController.navigationItem.titleView addSubview:testView];
to
self.navigationItem.titleView = testView;
Edit:
Note: You cannot add subviews to titleView cause it's default value is nil, you need to set a new view as the titleView.

Swift 3/4
You may set i.e. UILabel as a titleView. Call it in viewDidLoad():
private func setNavigationTitle(_ title: String) {
navigationItem.title = nil // clear the default title
let titleLabel = UILabel() // you don't need to specify a frame, it will be centred in the navbar
titleLabel.font = ...
titleLabel.textColor = ...
titleLabel.text = title
titleLabel.backgroundColor = .clear
navigationItem.titleView = titleLabel
navigationTitleView = titleLabel // you may create a property if you want to manipulate the title view later
}
Note navigationItem.title = nil, otherwise title may override titleView.

CustomLabel *titleLabel = [CustomLabel initWithLabelFrame:labelFrame textFont:[UIFont fontWithName:#"Helvetica" size:[UIFont systemFontSize]] textColor:[UIColor blackColor] labelText:#"Add as" textAlignment:NSTextAlignmentCenter labelOnView:reference.view labelTag:62];
[self.navigationItem setTitleView:titleLabel]; // titleLabel set in navigationItem

#import <UIKit/UIKit.h>
#interface MasterViewController : UITableViewController
#end
#import "MasterViewController.h"
#interface MasterViewController ()
#end
#implementation MasterViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.titleView = [self titleView];
}
- (UIView *)titleView {
CGFloat navBarHeight = self.navigationController.navigationBar.frame.size.height;
CGFloat width = 0.95 * self.view.frame.size.width;
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, navBarHeight)];
UIImage *logo = [UIImage imageNamed:#"logo.png"];
UIButton *logoButton = [UIButton buttonWithType:UIButtonTypeCustom];
CGFloat logoY = floorf((navBarHeight - logo.size.height) / 2.0f);
[logoButton setFrame:CGRectMake(0, logoY, logo.size.width, logo.size.height)];
[logoButton setImage:logo forState:UIControlStateNormal];
UIImage *bubble = [UIImage imageNamed:#"notification-bubble-empty.png"];
UIImageView *bubbleView = [[UIImageView alloc] initWithImage:bubble];
const CGFloat Padding = 5.0f;
CGFloat bubbleX =
logoButton.frame.size.width +
logoButton.frame.origin.x +
Padding;
CGFloat bubbleY = floorf((navBarHeight - bubble.size.height) / 2.0f);
CGRect bubbleRect = bubbleView.frame;
bubbleRect.origin.x = bubbleX;
bubbleRect.origin.y = bubbleY;
bubbleView.frame = bubbleRect;
[containerView addSubview:logoButton];
[containerView addSubview:bubbleView];
return containerView;
}
#end

You'll want to use storyboard to do this to support iPhone 6 and newer (larger) devices.
Create a container view for your custom navigation item title/subtitle etc, and drag it into the visual editor (not into the view hierarchy).

Swift 3
let myImageView = UIImageView(image: <...set your image...>)
override fun viewDidLoad(){
super.viewDidLoad()
self.navigationItem.titleView = myImageView //
}
One more alternate solution is
override fun viewWillAppear(_ animated: Bool) {
super. viewWillAppear(animated)
self.navigationItem.titleView = myImageView
}
I recommend to use, viewDidLoad to setup your titleView

Related

iOS tableviewheader subview not shown

I use a UITableView and I want to setup a text at the beginning.
My code is:
UIView *tableHeaderView = [[UILabel alloc]initWithFrame:CGRectMake(0.0, 0.0, self.tableView.frame.size.width, 20.0)];
tableHeaderView.backgroundColor = [UIColor groupTableViewBackgroundColor];
UILabel *tableHeaderLabel = [[UILabel alloc]initWithFrame:CGRectMake(15.0,0,tableHeaderView.frame.size.width-15.0,tableHeaderView.frame.size.height)];
tableHeaderLabel.text = #"Countries";
if([UIFont respondsToSelector:#selector(preferredFontForTextStyle:)])
tableHeaderLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];
else
tableHeaderLabel.font = [UIFont boldSystemFontOfSize:14.0];
[tableHeaderView addSubview:tableHeaderLabel];
self.tableView.tableHeaderView = tableHeaderView;
[tableHeaderView bringSubviewToFront:tableHeaderLabel];
[tableHeaderLabel release];
[tableHeaderView release];
The problem is that the text isn't shown.
If I suppress the backgroundColor of the main header view, or if I replace it with a transparent one as if the Label awas under the main header view.
So I added the line:
[tableHeaderView bringSubviewToFront:tableHeaderLabel];
but this doesn't fix the issue.
I can't use the Label directly as the tableViewHeaderView directly becaus I want to left space at the left of the text.
Anybody have an idea to help me?
Thanks.
The only problem you have here is a typo in the first line, initialized an UIView wrongly as an UILabel. The compiler will not compliant as UILabel is a subclass of UIView.
I ran the following code and it worked as expected: (The code is for ARC, if you are not using it, don't forget to do the release!)
- (void)viewDidLoad {
[super viewDidLoad];
UIView *tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0,
self.tableView.frame.size.width,
20.0)];
tableHeaderView.backgroundColor = [UIColor groupTableViewBackgroundColor];
UILabel *tableHeaderLabel = [[UILabel alloc] initWithFrame:CGRectMake(15.0, 0,
tableHeaderView.frame.size.width - 15.0,
tableHeaderView.frame.size.height)];
tableHeaderLabel.text = #"Countries";
tableHeaderView.backgroundColor = [UIColor groupTableViewBackgroundColor];
if([UIFont respondsToSelector:#selector(preferredFontForTextStyle:)]) {
tableHeaderLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];
} else {
tableHeaderLabel.font = [UIFont boldSystemFontOfSize:14.0];
}
[tableHeaderView addSubview:tableHeaderLabel];
self.tableView.tableHeaderView = tableHeaderView;
}

Navigation Bar's Title View Overlaps

I have added a custom titleView in Navigation Bar.
In every screen change I am adding another title View.
The problem is that the previous one is not get removed and I can See all Views at a time.
Here is my code:
UILabel *lblTitle = [[UILabel alloc] init];
lblTitle.text = text;
CGSize lblSize = [Utility sizeOfText:text withFont:kCGFontMedium(19)];
lblTitle.frame = CGRectMake(60, 9, lblSize.width, lblSize.height);
lblTitle.font =kCGFontMedium(19);
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor whiteColor];
[lblTitle sizeToFit];
[self.navigationController.navigationBar addSubview:lblTitle];
My Problem:
Sign Up and Sign In overlaps
Solution 1: each time you have to remove the custom UIView object added to the navigation bar
[self.navigationController.navigationBar.subviews enumerateObjectsWithOptions:0 usingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj isMemberOfClass:[UILabel class]]) {
[obj removeFromSuperview];
}
}];
UILabel *lblTitle = [UILabel new];
lblTitle.text = text;
CGSize lblSize = [Utility sizeOfText:text withFont:kCGFontMedium(19)];
lblTitle.frame = CGRectMake(60, 9, lblSize.width, lblSize.height);
lblTitle.font = kCGFontMedium(19);
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor whiteColor];
[lblTitle sizeToFit];
[self.navigationController.navigationBar addSubview:lblTitle];
Solution 2 : add the property in a class interface to store access to custom subview in the navigationBar
#property (weak, readwrite, nonatomic) UILabel *navSubView;
[self.lblTitle removeFromSuperview];
self.lblTitle = [UILabel new];
lblTitle.text = text;
CGSize lblSize = [Utility sizeOfText:text withFont:kCGFontMedium(19)];
lblTitle.frame = CGRectMake(60, 9, lblSize.width, lblSize.height);
lblTitle.font = kCGFontMedium(19);
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor whiteColor];
[lblTitle sizeToFit];
[self.navigationController.navigationBar addSubview:lblTitle];
Or you could just use the NavigationItem's "titleView" property
UILabel *titleLabel = [[UILabel alloc] init];
UIView *titleView = [[UIView alloc] initWithFrame:titleLabel.frame];[titleView addSubview:titleLabel];
self.navigationItem.titleView = titleView;
This will ensure only one instance of title label exists, and it won't overlap the self.title either

In ios 7 how to increase the size of UINavigation bar

Used storyboard to create navigation bar. and now in ios 7 i have a challenge to increase the height of navigation bar.
UINavigationBar *navBar = [[self navigationController] navigationBar];
[navBar setFrame:CGRectMake(0, 100, 1024, 200)];
UIFont *font = [UIFont fontWithName:#"fontName" size:YourFloatSize];
CGSize titleSize = [title sizeWithFont:font];
UILabel *titleLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, titleSize.width, titleSize.height)];
titleLable.text = title;
titleLable.textAlignment = NSTextAlignmentCenter;
titleLable.backgroundColor = [UIColor clearColor];
titleLable.textColor = [UIColor anyColorYouWant];
titleLable.font = font;
self.navigationItem.titleView = titleLable;
You can't change the height of the navigation bar. It's fixed at 44 pixels.
Use this -
#implementation UINavigationBar (customNavBar)
- (CGSize)sizeThatFits:(CGSize)size
{
CGSize navSize = CGSizeMake(self.frame.size.width,200);
return navSize;
}

Trying to give UILabel a shadow, but it won't show up

I'm trying to give a label in one of the classes in my app a drop shadow, but it's not working at all. Nothing shows up. What am I doing wrong?
// Set label properties
titleLabel.font = [UIFont boldSystemFontOfSize:TITLE_FONT_SIZE];
titleLabel.adjustsFontSizeToFitWidth = NO;
titleLabel.opaque = YES;
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textColor = titleLabelColor;
titleLabel.shadowColor = [UIColor blackColor];
titleLabel.shadowOffset = CGSizeMake(10, 10);
It's just white, no shadow.
Just Add this line to before adding titleLabel to self.view
titleLabel.layer.masksToBounds = NO;
Good Luck !!
i hope you are aware of categories?
Creating a category will be better option:
Command + N > Objective-C Category > Category = Animation & Category on = UIView
This will create 2 files with name UIView+Animation.h and UIView+Animation.m
UIView+Animation.h file
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#interface UIView (Animation)
- (void)setBackgroundShadow:(UIColor *)shadowColor CGSize:(CGSize)CGSize shadowOpacity:(float)shadowOpacity shadowRadius:(float)shadowRadius;
#end
UIView+Animation.m file
#import "UIView+Animation.h"
#implementation UIView (Animation)
- (void)setBackgroundShadow:(UIColor *)shadowColor CGSize:(CGSize)CGSize shadowOpacity:(float)shadowOpacity shadowRadius:(float)shadowRadius
{
self.layer.shadowColor = shadowColor.CGColor;
self.layer.shadowOffset = CGSize;
self.layer.shadowOpacity = shadowOpacity;
self.layer.shadowRadius = shadowRadius;
self.clipsToBounds = NO;
}
Import UIView+Animation.h in any of your viewController and call it like this:
[self.titleLabel setBackgroundShadow:[UIColor grayColor] CGSize:CGSizeMake(0, 5) shadowOpacity:1 shadowRadius:5.0];
Just make sure that you allocate the UILabel and also set a frame for the label. And also make sure that view is added to the subview. Something like this:
titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 30)];
titleLabel.font = [UIFont boldSystemFontOfSize:14];
titleLabel.adjustsFontSizeToFitWidth = NO;
titleLabel.opaque = YES;
titleLabel.text = #"My Label";
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textColor = [UIColor whiteColor];
titleLabel.shadowColor = [UIColor blackColor];
titleLabel.shadowOffset = CGSizeMake(5, 5);
[myView addSubview:titleLabel];
[titleLabel release];
value 10 for shadow offset is quite big. You can tweak the values based on your requirements.

removing uinavigationbar title margins

I modified my navigation bar as follows in app delegate:
NSDictionary *settings = #{
UITextAttributeFont : [UIFont fontWithName:#"impact" size:36.0],
UITextAttributeTextColor : [UIColor whiteColor],
UITextAttributeTextShadowColor : [UIColor clearColor],
UITextAttributeTextShadowOffset : [NSValue valueWithUIOffset:UIOffsetZero]};
[[UINavigationBar appearance] setTitleTextAttributes:settings];
But the font cuts down like this for the larger font. :
I tried doing this in viewWillAppear of my VC, :
UIView *newTitleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[newTitleView setBackgroundColor:[UIColor blackColor]];
[self.navigationController.navigationBar addSubview:newTitleView];
Then I was going to align title to center. But that just doesn't seems right. Only thing I actually need is to remove margins at top and bottom of the title Label. How do I go about it.
Try this code and don't set setTitleTextAttributes
UILabel *titleView = (UILabel *)self.navigationItem.titleView;
if (!titleView) {
titleView = [[UILabel alloc] initWithFrame:CGRectZero];
titleView.backgroundColor = [UIColor clearColor];
titleView.font = [UIFont boldSystemFontOfSize:20.0];
titleView.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
titleView.textColor = [UIColor yellowColor]; // Change to desired color
self.navigationController.navigationItem.titleView = titleView;
[titleView release];
}
titleView.text = title;
[titleView sizeToFit];
Well the easiest way i would suggest is to use a UIView with label and button customised for the requirement and use it,not on navigation bar.
mnavigation bar height customization is the trouble that the height is fixed and the subview also it may affect .So I suggest to go with a custom header view subclassed with UIView
You can subclass to the UILabel and override drawTextInRect: like this:
- (void)drawTextInRect:(CGRect)rect {
UIEdgeInsets insets = {0, 5, 0, 5};
return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
}
For more useful link you can find
here

Resources