I'm adding a title to a UIToolbar (iPad) with the following code:
- (void)setupToolbarItems {
if (!toolbarItems) {
UIBarButtonItem *flexSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 386.0, 20.0)];
titleLabel.autoresizingMask = (UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth);
titleLabel.textColor = [UIColor whiteColor];
titleLabel.text = #"Hello";
titleLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:20.0];
titleLabel.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5];
titleLabel.shadowOffset = CGSizeMake(0.0, -1.0);
titleLabel.textAlignment = UITextAlignmentCenter;
titleLabel.backgroundColor = [UIColor clearColor];
UIBarButtonItem *titleLabelItem = [[UIBarButtonItem alloc] initWithCustomView:titleLabel];
toolbarItems = [[NSArray alloc] initWithObjects:flexSpacer, titleLabelItem, flexSpacer, nil];
[titleLabel release];
[flexSpacer release];
[titleLabelItem release];
}
[self setItems:self.toolbarItems animated:NO];
}
Everything works fine in portrait, but in landscape mode the label is too far down. UIBarButtonItem isn't a view, so I have no way of adjusting its position from my code as far as I can see…how can I fix this?
Portrait:
Landscape:
Add UIViewAutoresizingFlexibleHeight to your titleView. You're not letting it shrink the label.
Related
I encountered a strange problem. I need to put two UILabel in UIToolbar. I have a button and I want to share with respect to its center the UILabel, as if I did not change the values of x and y inside UILabel up at the bottom, they do not move. Tell me what could be the problem. I need a little lower down the UILabel.
= [NSMutableArray new];
_createdLabel = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, 100, 20)];
_createdLabel.backgroundColor = [UIColor clearColor];
_createdLabel.textColor = [UIColor whiteColor];
UIFont *myFont = [ UIFont fontWithName:#"GothamPro-Light" size:15.0f ];
_createdLabel.font = myFont;
_createdLabel.text = #"";
UIBarButtonItem *createdTitle = [[UIBarButtonItem alloc] initWithCustomView:_createdLabel];
[_items addObject:createdTitle];
UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[_items addObject:space];
UILabel *shareLabel = [[UILabel alloc] initWithFrame: CGRectMake(0, -1000, 85, 20)];
shareLabel.backgroundColor = [UIColor clearColor];
shareLabel.textColor = [UIColor whiteColor];
UIFont *shareFont = [ UIFont fontWithName:#"GothamPro-Light" size:15.0f ];
shareLabel.font = shareFont;
shareLabel.text = #"Поделиться";
[shareLabel sizeToFit];
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(socialSharing:)];
[shareLabel setUserInteractionEnabled:YES];
[shareLabel addGestureRecognizer:tap];
UIBarButtonItem *shareTitle = [[UIBarButtonItem alloc] initWithCustomView:shareLabel] ;
[_items addObject:shareTitle];
UIBarButtonItem *shareBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(socialSharing:)];
shareBtn.tintColor = [UIColor whiteColor];
[_items addObject:shareBtn];
[self.toolBar setItems:_items animated:YES];
Please remove [shareLabel sizeToFit]; in your code.
Use below code for share label.
UILabel *shareLabel = [[UILabel alloc] initWithFrame: CGRectMake(0, 4, 85, 16)];
You try this code, it's working fine
UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 40)];
[toolBar setBarTintColor: self.navigationController.navigationBar.tintColor];
NSMutableArray *items= [NSMutableArray new];
UILabel *createdLabel = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, 50, 20)];
createdLabel.backgroundColor = [UIColor clearColor];
createdLabel.textColor = [UIColor whiteColor];
UIFont *myFont = [ UIFont fontWithName:#"GothamPro-Light" size:15.0f ];
createdLabel.font = myFont;
createdLabel.text = #"Hai";
UIBarButtonItem *createdTitle = [[UIBarButtonItem alloc] initWithCustomView:createdLabel];
[items addObject:createdTitle];
UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[items addObject:space];
UILabel *shareLabel = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, 85, 20)];
shareLabel.backgroundColor = [UIColor clearColor];
shareLabel.textColor = [UIColor whiteColor];
UIFont *shareFont = [ UIFont fontWithName:#"GothamPro-Light" size:15.0f ];
shareLabel.font = shareFont;
shareLabel.text = #"Hello "; //#"Поделиться";
[shareLabel sizeToFit];
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(socialSharing)];
[shareLabel setUserInteractionEnabled:YES];
[shareLabel addGestureRecognizer:tap];
UIBarButtonItem *shareTitle = [[UIBarButtonItem alloc] initWithCustomView:shareLabel] ;
[items addObject:shareTitle];
UIBarButtonItem *shareBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(socialSharing)];
shareBtn.tintColor = [UIColor whiteColor];
[items addObject:shareBtn];
[toolBar setItems:items animated:YES];
[self.view addSubview:toolBar];
provide method like this,
-(void)socialSharing
{
NSLog(#"Hai");
}
Definitely helps to you.
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
I have the following code in my viewDidLoad of my containing view:
// Now add the next button
UIBarButtonItem *nextButton = [[UIBarButtonItem alloc] initWithTitle:#"Next" style:UIBarButtonItemStylePlain target:self action:#selector(self)];
self.navigationItem.rightBarButtonItem.tintColor = [UIColor blueColor];
self.navigationItem.rightBarButtonItem = nextButton;
The UINavigationController parent has this in viewDidLoad:
[super viewDidLoad];
// Do any additional setup after loading the view.
self.navigationBar.tintColor = [UIColor whiteColor];
self.navigationBar.barTintColor = [UIColor blackColor];
How do I just customize the rightBar button to change the background color and maybe the text color?
You have to set the tintColor property on the button after you assign the button.
self.navigationItem.rightBarButtonItem = nextButton;
self.navigationItem.rightBarButtonItem.tintColor = [UIColor blueColor];
instead of
self.navigationItem.rightBarButtonItem.tintColor = [UIColor blueColor];
self.navigationItem.rightBarButtonItem = nextButton;
If you wish to have the same color for all bar buttons you can use the following line of code:
self.window.tintColor = [UIColor redColor];
in your app delegate.
Try below code you can set color of button as well text color
UIView *customBarButtonView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 30)];
UIButton *buttonDone = [UIButton buttonWithType:UIButtonTypeSystem];
[buttonDone addTarget:self action:#selector(doneButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
buttonDone.backgroundColor = [UIColor redColor];
// or try this one
// buttonDone.tintColor = [UIColor redColor];
buttonDone.frame = CGRectMake(10, 0, 50, 30);
buttonDone.titleLabel.textColor = [UIColor colorWithRed:141.0/255.0 green:209.0/255.0 blue:205.0/255.0 alpha:1.0];
buttonDone.titleLabel.font = [UIFont fontWithName:#"SourceSansPro-Light" size:20.0f];
[buttonDone setTitle:#"Done" forState:UIControlStateNormal];
[customBarButtonView addSubview:buttonDone];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:customBarButtonView];
working code.. try this.
UIImage *image = [UIImage imageNamed:#"Image.png"];
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:#selector(back:)];
self.navigationItem.leftBarButtonItem = menuButton;
I have a UICollectionViewController and trying to set navigation title..
but none of these works...It does not show the text!
self.titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
self.titleLabel.backgroundColor = [UIColor clearColor];
self.titleLabel.font = [UIFont fontWithName:#"ArialHebrew-Bold" size:15];
self.titleLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
self.titleLabel.textAlignment = NSTextAlignmentCenter;
self.titleLabel.textColor = [UIColor whiteColor];
self.titleLabel.text = [self.navigationItem title];
self.navigationItem.titleView = self.titleLabel;
[self.titleLabel setText:#"title"];
self.navigationItem.title = #"The title";
Make the frame of your new label bigger. The height of a nav bar is fixed at 44 pixels.
For example: CGRectMake(100,0,120,44);
The width and origins depend on what else is on the navbar, and the width of the navbar.
Is there a way to change the size of the font in a UIToolbar? My text does not fit so I need to make it smaller.
Thanks!
Here is one example. I am not sure what you want, but you can resize the font size of uibarbuttonitem in uitoolbar.
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,10,15)];
label.font = [UIFont systemFontOfSize:15.0];
label.textColor = [UIColor blackColor];
label.backgroundColor = [UIColor clearColor];
//label.adjustsFontSizeToFitWidth = YES;
label.textAlignment = UITextAlignmentCenter;
label.text = #"Bar";
UIBarButtonItem *item2 = [[UIBarButtonItem alloc]
initWithCustomView:label];