Navigation Bar's Title View Overlaps - ios

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

Related

How to align custom UINavigationBar title (UILabel )in middle (iOS)

I am using following code to align 2 strings in UINavigationBar centre .
for ex:
<Back John Kelly Maria
23/F
I need these kind of middle aligned text in UINavigationbar but what jam now getting is all the text aligned in right side of UINavigationBar .
something like this
<Back John Kelly Maria
23/F
Please help..me .
this is my code
UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,50)];
UIFont * customFont = [UIFont fontWithName:#"Helvetica Neue" size:19]; //custom font
NSString * text =title;
UILabel *patientNameLabel;
if([title length]>15){
patientNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,320,30)];
}else{
patientNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,320,30)];
}
patientNameLabel.text = #"John Kelly Maria";
patientNameLabel.font = customFont;
patientNameLabel.numberOfLines = 0;
patientNameLabel.textAlignment=NSTextAlignmentCenter;
patientNameLabel.minimumScaleFactor = 10.0f/12.0f;
patientNameLabel.clipsToBounds = YES;
patientNameLabel.backgroundColor = [UIColor clearColor];
patientNameLabel.textColor = [UIColor blackColor];
patientNameLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight;
NSString *subTitle =subTitleText;
UIFont * customFont1 = [UIFont fontWithName:#"Helvetica Neue" size:14]; //custom font
UILabel *subTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,25,320,20)];
subTitleLabel.textAlignment = NSTextAlignmentCenter;
subTitleLabel.text = #"23/F";
subTitleLabel.font = customFont1;
subTitleLabel.textAlignment=NSTextAlignmentCenter;
subTitleLabel.numberOfLines = 1;
subTitleLabel.adjustsFontSizeToFitWidth = YES;
subTitleLabel.minimumScaleFactor = 10.0f/12.0f;
subTitleLabel.clipsToBounds = YES;
subTitleLabel.backgroundColor = [UIColor clearColor];
subTitleLabel.textColor = [UIColor blackColor];
[titleView addSubview:patientNameLabel];
[titleView addSubview:subTitleLabel];
Add below two lines before addSubview lines..
patientNameLabel.center = CGPointMake(titleView.frame.size.width/2, 0);
subTitleLabel.center = CGPointMake(titleView.frame.size.width/2, 25);
Add Below Code & see
UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width,50)];
UIFont * customFont = [UIFont fontWithName:#"Helvetica Neue" size:19]; //custom font
NSString * text =#"Nilesh Patel";
UILabel *patientNameLabel;
if([text length]>15){
patientNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,320,30)];
}else{
patientNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,320,30)];
}
patientNameLabel.text = #"Nilesh Patel";
patientNameLabel.font = customFont;
patientNameLabel.numberOfLines = 0;
patientNameLabel.textAlignment=NSTextAlignmentCenter;
patientNameLabel.minimumScaleFactor = 10.0f/12.0f;
patientNameLabel.clipsToBounds = YES;
patientNameLabel.backgroundColor = [UIColor clearColor];
patientNameLabel.textColor = [UIColor blackColor];
patientNameLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight;
NSString *subTitle =#"iOS Developer";
UIFont * customFont1 = [UIFont fontWithName:#"Helvetica Neue" size:14]; //custom font
UILabel *subTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,25,320,20)];
subTitleLabel.textAlignment = NSTextAlignmentCenter;
subTitleLabel.text = #"28/M";
subTitleLabel.font = customFont1;
subTitleLabel.textAlignment=NSTextAlignmentCenter;
subTitleLabel.numberOfLines = 1;
subTitleLabel.adjustsFontSizeToFitWidth = YES;
subTitleLabel.minimumScaleFactor = 10.0f/12.0f;
subTitleLabel.clipsToBounds = YES;
subTitleLabel.backgroundColor = [UIColor clearColor];
subTitleLabel.textColor = [UIColor blackColor];
patientNameLabel.center = CGPointMake(titleView.frame.size.width/2, 10);
subTitleLabel.center = CGPointMake(titleView.frame.size.width/2, 30);
[titleView addSubview:patientNameLabel];
[titleView addSubview:subTitleLabel];
[self.navigationController.navigationBar addSubview:titleView];
Note : You have set static frame to titleView so if you see your app on different device you will not be able to see the text in exact centre. To avoid this set the titleView frame dynamically based on device/view width.
Try Frame setting like this
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0,20,screenWidth,50)];
UILabel *subTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,25,screenWidth,20)];
UILabel *patientNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,screenWidth,30)];

How to add multiple UILabels programmatically in Xcode?

Hello in Xcode I am trying to add two labels to my image programmatically but when I add the second one it will overwrite my first one. Here is my code:
//first label
UILabel* caption = [[UILabel alloc] initWithFrame:
CGRectMake(-25, 0, 320, 50)
];
caption.backgroundColor = [UIColor whiteColor];
caption.textColor = [UIColor blackColor];
caption.textAlignment = UITextAlignmentLeft;
caption.font = [UIFont systemFontOfSize:16];
caption.text = [NSString stringWithFormat:#" #%#",[data objectForKey:#"username"]];
[self addSubview: caption];
//second label
UILabel* bottomBox = [[UILabel alloc] initWithFrame:
CGRectMake(-25, -200, 320, 50)
];
caption.backgroundColor = [UIColor whiteColor];
caption.textColor = [UIColor blackColor];
caption.textAlignment = UITextAlignmentCenter;
caption.font = [UIFont systemFontOfSize:16];
caption.text = [NSString stringWithFormat:#" Testing"];
[self addSubview: bottomBox];
I've tried changing the "0 to -200 to modify the Y coordinates so that the second label will shift down but it just overwrites the first one for some reason. Any help would be greatly appreciated thanks.
All the code for your 2nd label is referencing the 1st variable caption instead of bottomBox. You want:
//second label
UILabel* bottomBox = [[UILabel alloc] initWithFrame:CGRectMake(20, 200, 320, 50)];
bottomBox.backgroundColor = [UIColor whiteColor];
bottomBox.textColor = [UIColor blackColor];
bottomBox.textAlignment = UITextAlignmentCenter;
bottomBox.font = [UIFont systemFontOfSize:16];
bottomBox.text = #"Testing";
[self addSubview: bottomBox];

how to Remove Dynamically Created UIView and UILabel text?

I am adding some dynamic UIView named as *cellSeparator and other UILabels...now what happen is when i again call this code then its rewrite the label text and overwrite on previously created label text...i am not very much aware to this ios development.so can anyone please tell me how can i remove this UIView dynamically before creating again?beacause UIView is dynamically created i dont know how to remove that UIview
UILabel *indexLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, self.view.frame.size.height-150, self.view.frame.size.width/2,30)];
[indexLabel setBackgroundColor:[UIColor clearColor]];
indexLabel.textColor = [UIColor whiteColor];
indexLabel.text = #"Details:-";
indexLabel.font = [UIFont systemFontOfSize:20.00];
UILabel *tagLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, self.view.frame.size.height-120, self.view.frame.size.width/2, 30)];
tagLabel.backgroundColor = [UIColor clearColor];
NSLog(#"LOg %#",imageId);
NSLog(#"LOg %#",imageStyle);
NSLog(#"LOg %#",imageType);
NSLog(#"LOg %#",imageWeight);
tagLabel.text = [NSString stringWithFormat:#"The Id of Jewl Is: %#",imageId];
imageTypelabel= [[UILabel alloc] initWithFrame:CGRectMake(20, self.view.frame.size.height-90, self.view.frame.size.width/2, 30)];
imageTypelabel.backgroundColor = [UIColor clearColor];
imageTypelabel.text = [NSString stringWithFormat:#"The Type of Jewl Is: %#",imageType];
imageStylelabel = [[UILabel alloc] initWithFrame:CGRectMake(20, self.view.frame.size.height-60, self.view.frame.size.width/2, 30)];
imageTypelabel.backgroundColor = [UIColor clearColor];
imageStylelabel.text = [NSString stringWithFormat:#"The style of Jewl Is: %#",imageStyle];
imageWeightlabel = [[UILabel alloc] initWithFrame:CGRectMake(20, self.view.frame.size.height-30, self.view.frame.size.width/2, 30)];
imageStylelabel.backgroundColor = [UIColor clearColor];
imageWeightlabel.text = [NSString stringWithFormat:#"The weight of Jewl Is: %#",imageWeight];
imageWeightlabel.backgroundColor = [UIColor clearColor];
imageWeightlabel.textColor = [UIColor whiteColor];
imageTypelabel.textColor = [UIColor whiteColor];
imageWeightlabel.textColor = [UIColor whiteColor];
tagLabel.textColor = [UIColor whiteColor];
UIImage *imageBegin = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:imageBegin];
UIView *cellSeparator = [[UIView alloc] initWithFrame:CGRectMake(0,545, self.view.frame.size.width ,3)];
cellSeparator.tag=1;
[cellSeparator setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleWidth];
[cellSeparator setContentMode:UIViewContentModeTopLeft];
[cellSeparator setBackgroundColor:[UIColor whiteColor]];
[self.view addSubview:cellSeparator];
You could write a method to remove all the subviews of the view and modify this code according to your need.
- (void)removeSubviewsOfView
{
NSArray *subViews = [self.view subviews];
for(UIView *view in subViews)
{
[view removeFromSuperview];
}
}

How to vertically align a UILabel used as a leftView in a UITextField with the textField's text?

I'm using a UILabel as the leftView of a UITextField. The issue is that the textField's text is higher than the label's.
This is the code I've used so far
UILabel *startsWith = [[UILabel alloc] init];
startsWith.font = [UIFont systemFontOfSize:14];
startsWith.textColor = [UIColor blackColor];
startsWith.backgroundColor = [UIColor clearColor];
startsWith.text = #"Text";
[startsWith sizeToFit];
self.textField.leftViewMode = UITextFieldViewModeAlways;
self.textField.leftView = startsWith;
I've tried slightly changing the label's frame but it didn't work...
How can I align both texts?
You could create a container view in which you position the UILabel 1px up.
UIView * v = [[UIView alloc] init];
v.backgroundColor = [UIColor clearColor];
UILabel *startsWith = [[UILabel alloc] init];
startsWith.font = self.textfield.font;
startsWith.textAlignment = self.textfield.textAlignment;
startsWith.textColor = [UIColor blackColor];
startsWith.backgroundColor = [UIColor clearColor];
startsWith.text = #"Text";
[startsWith sizeToFit];
startsWith.frame = CGRectOffset(startsWith.frame, 0, -1);
v.frame = startsWith.frame;
[v addSubview:startsWith];
self.textfield.leftViewMode = UITextFieldViewModeAlways;
self.textfield.leftView = v;

Customize navigation bar with title view

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

Resources