UIBarButtonSystemItemFlexibleSpace - ios

Apple docs have this to say:
UIBarButtonSystemItemFlexibleSpace
Blank space to add between other items. The space is distributed equally between the other items. Other item properties are ignored when this value is set.
That's a little vague (exactly what space is distributed equally?) So I wrote a test method:
-(void) createToolbar {
BOOL stuffInTopLeftCorner = NO;
UIToolbar* bar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 1024, 44)];
self.bar = bar;
[self addSubview:bar];
UILabel* titleLabel = [[UILabel alloc]initWithFrame:CGRectZero];
titleLabel.text = #"Centered title";
titleLabel.font = [UIFont systemFontOfSize:30];
[titleLabel sizeToFit];
CGSize titleSize = [titleLabel bounds].size;
NSLog(#"titleSize is %g %g", titleSize.width, titleSize.height);
UIBarButtonItem* titleItem = [[UIBarButtonItem alloc]initWithCustomView:titleLabel];
UIBarButtonItem* flexibleSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UILabel* leftLabel = [[UILabel alloc]initWithFrame:CGRectZero];
leftLabel.text = #"Stuff in top left corner";
leftLabel.font = [UIFont systemFontOfSize:30];
[leftLabel sizeToFit];
UIBarButtonItem* topLeftCornerItem = [[UIBarButtonItem alloc]initWithCustomView:leftLabel];
NSMutableArray* items = [NSMutableArray arrayWithObjects: flexibleSpace, titleItem, flexibleSpace, nil];
if (stuffInTopLeftCorner) {
[items insertObject:topLeftCornerItem atIndex:0];
}
bar.items = items;
}
Here is what it looks like with the code as above:
And here is what it looks like if I change stuffInTopLeftCorner to YES:
It appears that adding something to the left of the title did not cause said title to move at all.
My question is -- does that mean it will always center the title, regardless of what goes on either side of it?

As best I can tell, yes, it always centers the title, assuming that is possible.

Related

UIToolbar UILabel x y offset not working

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.

Navigation bar right bar button item is not visible

- (void)setRightNavigationBarViewForUser {
UIBarButtonItem *spacer = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil action:nil];
spacer.width = 760;
NSString *title = [VimondStore sessionManager].userName;
UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 144, 44)];
tempView.backgroundColor = [UIColor clearColor];
UIImageView *tempImageView = [[UIImageView alloc] initWithFrame:CGRectMake(4, 0, 44, 44)];
tempImageView.image = [UIImage imageNamed:#"user.png"];
UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(44, 0, 80, 44)];
tempLabel.backgroundColor = [UIColor clearColor];
tempLabel.text = title;
tempLabel.font = [UIFont boldSystemFontOfSize:14.0];
tempLabel.textColor = [UIColor whiteColor];
tempLabel.textAlignment = NSTextAlignmentLeft;
tempLabel.adjustsFontSizeToFitWidth = YES;
tempLabel.minimumScaleFactor = 0.8;
[tempView addSubview:tempImageView];
[tempView addSubview:tempLabel];
UIBarButtonItem *userView = [[UIBarButtonItem alloc]initWithCustomView:tempView];
NSArray *items = #[spacer ,userView];
self.navigationTableViewController.navigationItem.rightBarButtonItems = items;
}
- (void)navigateToHome {
[self setRightNavigationBarViewForUser];
self.loginViewController = nil;
[self showCenterPanelAnimated:YES];
[self setLeftBarButtonForDrawerTitle];
NSAssert([self.centreViewController isKindOfClass:[GGBaseViewController class]], #"Must be of GGBaseViewController class");
[GenreNavigator navigateToRoot:(GGBaseViewController*)self.centreViewController completionHandler:nil];
}
My code is given above: The problem, I am facing is that navigation right bar button items are not visible first time when I navigate to home. When I navigate to some other page and comes back then its visible. The first method is used to create right navigation bar button items.
From Apple doc on rightBarButtonItems, you can see that most likely your custom view is too wide and your button is not showing because it does not fit. Test making it narrower and see if it appears?
Discussion
This array can contain 0 or more bar button items to display on the right side of the navigation bar. Items are displayed right-to-left in the same order as they appear in the array. Thus, the first item in the array is the rightmost item and other items are added to the left of the previous item.
If there is not enough room to display all of the items in the array, those that would overlap the title view (if present) or the buttons on the left side of the bar are not displayed.

Show the UIToolbar's UIBarButtonItem in the right side, instead of the left side

Can someone tell me why my button is on the left side instead of the right? I would like it on the right. Thanks for any help you can give!
- (IBAction)getSelection:(id)sender {
if(self.pickerShown == NO){
self.slidePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0,0, 320, 216)];
self.slidePicker.datePickerMode = UIDatePickerModeTime;
self.slidePicker.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.slidePicker];
//make done button
self.toolBar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,320,44)];
self.toolBar.barTintColor = [UIColor whiteColor];
self.barButtonDone = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStylePlain target:self
action:#selector(testButton)];
self.toolBar.items = [[NSArray alloc] initWithObjects:self.barButtonDone,nil];
self.navigationItem.rightBarButtonItem = self.barButtonDone;
self.barButtonDone.tintColor=[UIColor blackColor];
self.pickerShown = YES;
[self.view addSubview:self.toolBar];
}
else {
return;
}
}
You need to add:
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
And change:
self.toolBar.items = [[NSArray alloc] initWithObjects: flexSpace, self.barButtonDone,nil];
I thing that you can:
//self.navigationItem.rightBarButtonItem = self.barButtonDone;
Information:
UIBarButtonSystemItemFlexibleSpace is a blank space to add between other items. The space is distributed equally between the other items. Other item properties are ignored when this value is set.
Apple documentation: UIBarButtonItem Class Reference
Also, you have the Flexible Space Bar Button Item in Xcode Interface Builder:

Why my UIButton doesn't display in my subview? iPad App

Could somebody help me to understand why my UIButton doesn't display in my custom UIView?
In the code below I posted initWithFrame method:
- (id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor =[UIColor whiteColor];
UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0,frame.size.width, 50)];
toolbar.backgroundColor = [UIColor grayColor];
UIBarButtonItem *cancelBarButton = [[UIBarButtonItem alloc]initWithTitle:#"Cancel" style:UIBarButtonItemStylePlain target:self action:#selector(dismissNewInvoiceView:)];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *saveBarButton = [[UIBarButtonItem alloc] initWithTitle:#"Save" style:UIBarButtonItemStylePlain target:self action:#selector(saveNewInvoice:)];
[saveBarButton setEnabled:NO];
NSArray *arrayOfItems = [[NSArray alloc]initWithObjects:cancelBarButton,flexibleSpace, saveBarButton, nil];
[toolbar setItems:arrayOfItems];
UILabel *headerViewLabel = [[UILabel alloc]initWithFrame:CGRectMake(335, 15, 200, 20)];
headerViewLabel.text = #"New Invoice";
[toolbar addSubview:headerViewLabel];
[self addSubview:toolbar];
UIButton *selectCustomerButton = [[UIButton alloc]initWithFrame:CGRectMake(25, 200, 60, 60)];
selectCustomerButton.imageView.image = [UIImage imageNamed:#"Farmer.png"];
[self addSubview:selectCustomerButton];
[self bringSubviewToFront:selectCustomerButton];
UILabel *invoiceNumberLabel = [[UILabel alloc]initWithFrame:CGRectMake(25, 55, 150, 25)];
[invoiceNumberLabel setFont:[UIFont fontWithName:#"Verdana" size:14]];
invoiceNumberLabel.text = #"Invoice number:";
[self addSubview:invoiceNumberLabel];
UITextField *invoiceNumberField = [[UITextField alloc]initWithFrame:CGRectMake(140, 53, 150, 30)];
[invoiceNumberField setFont:[UIFont fontWithName:#"Verdana" size:25]];
invoiceNumberField.placeholder = #"25/13";
[self addSubview:invoiceNumberField];
UILabel *dateOfCreationLabel = [[UILabel alloc]initWithFrame:CGRectMake(500, 55, 150, 25)];
[dateOfCreationLabel setFont:[UIFont fontWithName:#"Verdana" size:14]];
dateOfCreationLabel.text = #"Date of creation:";
[self addSubview:dateOfCreationLabel];
}
return self;}
Why UILabels are displaying properly by implement addSubview method while UIButtons are not?
Regards
Don't do this:
selectCustomerButton.imageView.image = [UIImage imageNamed:#"Farmer.png"]
Use
[selectCustomerButton setImage:[UIImage imageNamed:#"Farmer.png"] forState:UIControlStateNormal];
The button sets its own image views image depending on its state, you don't access and set the image view's image yourself.
The button will be just invisible in case the image is not loaded/shown properly. As jrturton already said, you should use another method to set the image.
To figure out if the button is 'there', you can give the button a backgroundcolor like this:
selectCustomerButton.backgroundColor = [UIColor redColor];
If you see a red box, you know the image is not properly loaded or maybe not even found.

Is there any such thing like CSS text-transform:uppercase for iOS UILabel

I have a UINavigationBar which title I would like to capitalize all letters so 'word' becomes 'WORD'.
I would like to do the same for buttons within this nav bar.
I was wondering if is there anything like CSS text-transform:uppercase; attribute where it is possible to change the visual appearance of the word but once retrieving its value it stills returning its raw value.
Thank you in advance
This is actually simpler than you would think. You can use NSString's uppercaseString to achieve this.
[myLabel setText:[myString uppercaseString]];
Or, better suited to your needs:
for (UIBarButtonItem *button in self.navigationItem.rightBarButtonItems) { //Your nag bar
[button setTitle:[[button title] uppercaseString]];
}
EDIT: Example of string not changing.
NSString *myString = #"hello";
NSString *mySecondString = #"world!";
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:[myString uppercaseString] style:UIBarButtonItemStyleBordered target:self action:nil];
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:[mySecondString uppercaseString] style:UIBarButtonItemStyleBordered target:self action:nil];
[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:item1,item2, nil]];
NSLog(#"%# | %#",myString,mySecondString);//this will output the same as when it was originally created.
As using [myString uppercaseString] has the downside of returning an uppercase string (well, it is not actually a downside ..) and not the original string, one option would be to set a UILabel with an all-caps font (Copperplate is just an example of an all-caps font, you will likely find a better one):
int height = navigationController.navigationBar.frame.size.height;
int width = navigationController.navigationBar.frame.size.width;
UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, height)];
navLabel.backgroundColor = [UIColor clearColor];
navLabel.textColor = [UIColor whiteColor];
navLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
navLabel.font = [UIFont fontWithName:#"Copperplate" size:18];
navLabel.textAlignment = UITextAlignmentCenter;
self.navigationItem.titleView = navLabel;
There is !
NSString *myLowercaseString = #"a lowercase string";
NSString *myUppercaseString = [myLowercaseString uppercaseString];
If you NSLog (#"%#", myUppercaseString);, you will get the following output :
A LOWERCASE STRING
Then you can set your button's or your navigation bar's text property to your string.

Resources