Adding two buttons to rightBarButtonItems not working - objective-c

I am not sure why the following code is not working:
UIBarButtonItem *newButton = [[UIBarButtonItem alloc] initWithTitle:#"New" style:UIBarButtonItemStyleBordered target:self action:#selector(newClicked:)];
UIBarButtonItem *shareButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(share:)];
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:newButton, shareButton, nil];
It only display the "newButton" UIBarButtonItem on the navigation bar and not the "shareButton" button.

I found a different issue involving this behavior.
If you've set a custom titleView (navigationItem.titleView = ...) then the navigation item can sometimes hide your buttons except the first one if it doesn't have room.
So make sure you limit the width of your custom titleView.

rightBarButtonItems is in ios 5.
I think the issue is with the objet name new, please change this to any other name like newButton or something like that.
Because new is a keyword used for memory allocation in C++

Looks like the problem had to do with code following the questioned code:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 400, 44)];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont fontWithName:#"Marker Felt" size:26.0];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
label.textAlignment = UITextAlignmentCenter;
label.textColor =[UIColor whiteColor];
label.text=self.title;
self.navigationItem.titleView = label;
[label release];
Posting this now in case someone else encounters this problem!

Related

navigation menu not at full width of screen

I'm writing an app in objective C for ios 8, and my navigation bar has an annoying margin thats causing it to be shifted to the right ~20 pixels for leftBarButtonItem and ~20 left for rightBarButtonItem, as shown in the image below. I have tried using negative spacers per some other posts to no avail. Any ideas?
(I'm trying to do this without using storyboards or xibs.)
Here is the code:
UIToolbar* toolbar_temp = [[UIToolbar alloc] init];
toolbar_temp.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 45);
toolbar_temp.barTintColor= [UIColor blackColor];
NSMutableArray *items = [[NSMutableArray alloc] init];
UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIImage *camimage = [UIImage imageNamed:#"cancel"];
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithImage:camimage style:UIBarButtonItemStylePlain target:self action:nil];
[items addObject:spaceItem ];
[items addObject:customItem ];
[items addObject:spaceItem ];
[toolbar_temp setItems:items animated:NO];
UIBarButtonItem *allButton = [[UIBarButtonItem alloc] initWithCustomView:toolbar_temp];
self.navigationItem.leftBarButtonItem = allButton;
EDIT:
I'm not sure if this gives any hints, but for some reason this works:
[[UINavigationBar appearance] setBarStyle:UIBarStyleBlackTranslucent];
While this does not:
[self.navigationController.navigationBar setBarStyle:UIBarStyleBlackTranslucent];
SOLUTION:
I ended up finding the solution myself. The issue was that I have a tabbar controller which had a navigation controller as one of the views. The navigation controller can only be setup in viewDidload, which doesn't get called in tabbar controllers. The solution was to add viewDidAppear and add the navigation controller within that function.
- (void)viewDidAppear:(BOOL)animated{[self addToolbar];}
Thanks all for the help!
It seems you cannot alter the minimum margins. You can do is add the toolbar_temp to the navigation bar:
[self.navigationController.navigationBar addSubview:toolbar_temp];
Or add to the self.view:
self.navigationController.navigationBarHidden = YES;
[self.view addSubview:toolbar_temp];

create and hide UILabel

I created a UILabel programatically and triggering a button I would like to hide the same label. This is my code:
UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 320, 100)];
nameLabel.text = #"TEXT";
nameLabel.backgroundColor = [UIColor greenColor];
nameLabel.numberOfLines = 5;
nameLabel.font = [UIFont boldSystemFontOfSize:12];
[self.view addSubview:nameLabel];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:#"Hide" style:UIBarButtonItemStyleBordered target:self action:#selector(back)];
- (IBAction)back{
self.navigationItem.rightBarButtonItem=nil;
[nameLabel setHidden: YES]; not working
nameLabel.hidden = YES; not working
}
Am I missing something?
This is also another way to do same
UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 320, 100)];
nameLabel.text = #"TEXT";
nameLabel.tag = 1001;
nameLabel.backgroundColor = [UIColor greenColor];
nameLabel.numberOfLines = 5;
nameLabel.font = [UIFont boldSystemFontOfSize:12];
[self.view addSubview:nameLabel];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:#"Hide" style:UIBarButtonItemStyleBordered target:self action:#selector(back)];
- (IBAction)back{
self.navigationItem.rightBarButtonItem=nil;
UILabel *tempLabel = (UILabel *)[self.view viewWithTag:1001];
[tempLabel setHidden: YES];
tempLabel.hidden = YES;
}
In order for the button to be accessible from other methods, you need to assign it to an instance variable (whether directly or via a property) rather than assigning it to a local variable. The proper way to declare the property is
#property(nonatomic, strong) UILabel *nameLabel;
which you can then assign to using
self.nameLabel = [[UILabel alloc] init...];
Later on, you can say
self.nameLabel.hidden = YES;
and it should work.
It's hard to know how that would even compile since the code you show to create nameLabel makes it local to whatever method that's in. Try making nameLabel a property and using self.nameLabel whenever you reference it, either creating it or touching its properties.

ios "Data Loading" Notification with transparent background

I am trying to create a view in iOS to let the user know their data is loading... it should have about a 200x200 Rounded-corner box in the middle with a spinner and the words "Data Loading..." and a transparent background.
This all is working except my 200x200 rounded-corner box is also transparent.
Here is the code I am using:
UIView *loadingDataView = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 320, 367)];
loadingDataView.alpha = 0.4;
loadingDataView.backgroundColor = [UIColor clearColor];
UIView *viewWithSpinner = [[UIView alloc] initWithFrame:CGRectMake(110, 106, 100, 100)];
[viewWithSpinner.layer setCornerRadius:15.0f];
viewWithSpinner.backgroundColor = [UIColor blackColor];
UILabel *msg = [[UILabel alloc] initWithFrame:CGRectMake(5, 75, 90, 20)];
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(5, 5, 90, 70)];
spinner.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
[spinner startAnimating];
msg.text = #"Data Loading";
msg.font = [UIFont systemFontOfSize:14];
msg.textAlignment = UITextAlignmentCenter;
msg.textColor = [UIColor whiteColor];
msg.backgroundColor = [UIColor clearColor];
viewWithSpinner.opaque = NO;
viewWithSpinner.backgroundColor = [UIColor blackColor];
[viewWithSpinner addSubview:spinner];
[viewWithSpinner addSubview:msg];
[loadingDataView addSubview:viewWithSpinner];
[self.view addSubview:loadingDataView];
Thanks.
No exactly an answer to your question, but check out the open source MBProgressHUD. It aims to be an open source replacement for the private UIProgressHUD class, and is exactly what you're trying to do.
The solution for your problem is that you should remove the alpha attribute of 0.4, that's what's turning your round view transparent, if your view is set to clearColor (this is not really a color, it just makes the view transparent) it makes no sense to add an alpha of 0.4. If what you want is a semi-transparent view surrounding your black rounded view, you should do the following:
[loadingDataView setBackgroundColor:[UIColor colorWithRed:1 green:1 blue:1 andAlpha:0.4]];
That will give you something whiteish kinda grayish, that should work.
However, I would recommend you to use the GIDAAlertView Class I developed, you can get the source and an example app on my GitHub:
It takes about 3 lines to get it working:
GIDAAlertView *spinnerAlert=[[GIDAAlertView alloc] initAlertWithSpinnerAndMessage:#"GIDAAlertView Spinner"];
//Show it ...
[spinnerAlert presentAlertWithSpinner];
//Later in your code, hide it
[spinnerAlert hideAlertWithSpinner];
This is how it looks like.
you are telling it to be clear...
loadingDataView.backgroundColor = [UIColor clearColor];
What do you want it to be black?

spacing rightBarButtonItems in UINavigationBar

i am using the rightBarButtonItems property of UINavigationBar to add two buttons on the right part of my navigation bar. is it possible to make the spacing between these two buttons wider?
thanks
You can add an UIBarButtonSystemItemFlexibleSpace item between your two buttons.
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
I was not able to get the flexible space to work in my situation but here is the code I used to be able to position the rightBarButtonItem: Note, I put a border around the UIView so you can see what it looks like with having the image in there.
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(89,40,100,30)];
containerView.layer.borderColor = [[UIColor redColor] CGColor];
containerView.layer.borderWidth = 1.0;
UIImage *image = [UIImage imageNamed:#"nav-icon.png"];
UIButton *navigationButton = [UIButton buttonWithType:UIButtonTypeCustom];
[navigationButton setFrame:CGRectMake(67,0,25,25)];
[navigationButton setImage:image forState:UIControlStateNormal];
[containerView addSubview:navigationButton];
UIBarButtonItem *navigationBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:containerView];
self.navigationItem.rightBarButtonItem = navigationBarButtonItem;

How to set the title of UIToolBar?

How can I set the title of UIToolBar such that it looks the same as the title in UINavigationBar?
I tried to use a button with plain style, it looks ok, but it will be highlighted when I click on it... Is there any better way to set the title in the detail view of split view?
This is what I use to present a title on a toolbar that will not highlight when pressed:
#define UIColorFromRGB(rgbValue) [UIColor \
colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
// choose whatever width you need instead of 600
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 600, 23)];
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
label.shadowColor = UIColorFromRGB(0xe5e7eb);
label.shadowOffset = CGSizeMake(0, 1);
label.textColor = UIColorFromRGB(0x717880);
label.text = #"your title";
label.font = [UIFont boldSystemFontOfSize:20.0];
UIBarButtonItem *toolBarTitle = [[UIBarButtonItem alloc] initWithCustomView:label];
[label release];
I think this would be much cleaner:
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:frame];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:#"Your Title"
style:UIBarButtonItemStylePlain
target:nil
action:nil];
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
NSArray *items = [[NSArray alloc] initWithObjects:spacer, item, spacer, nil];
[toolbar setItems:items];
toolbar.userInteractionEnabled = NO;
This solves the highlighting problem, disabled problem, and touch problem.
Toolbar and titleButton were both created in IB.
The view title is covered by the toolbar. So put the title in a toolbar.
self.titleButton.title = #"Order"; // myInterestingTitle
It looks like this:
Disable to prevent any highlighting, and to stop it from responding to touches.
self.titleButton.enabled=NO;
Then it looks like this:
It will look disabled, so set the color for disabled to white,
which has an implicit alpha=1.0.
This effectively overrides the 'disabled' look.
[self.titleButton setTitleTextAttributes:
[NSDictionary dictionaryWithObject:[UIColor whiteColor]
forKey:UITextAttributeTextColor]
forState:UIControlStateDisabled ];
Here's what you get:
Swift 5 version of Rayfleck's great answer:
let titleButton = UIBarButtonItem(title: "My Title", style: .plain, target: nil, action: nil)
titleButton.isEnabled = false
titleButton.setTitleTextAttributes([.foregroundColor : UIColor.black], for: .disabled)
Add this button to the toolbar as normal.
Put the regular "Rounded Rect Button" (UIButton) on toolbar (not "Bar Button Item")! Be aware, that behind the scene, IB wrap UIButton with UIBarButtonItem. It is why you have to click on button twice to get to UIButton properties (true for Xcode 4.2).
Get to UIButton properties. When you click first time, you will get "Bar button item" properties (it is what IB created automatically for you). Now click second time, you'll get to "Button" properties (also, when you selected UIButton you can't resize it and can't see resize markers).
In the Button properties set type to "Custom" to remove rounded rect border around button (you may need to resize button in IB to refresh it before you can see difference).
In the Button properties under "View" uncheck "User Interaction Enabled".
UILabel* title = [[[UILabel alloc] init] autorelease];
[title setBackgroundColor:[UIColor clearColor]];
[title setFont:[UIFont boldSystemFontOfSize:20]];
[title setTextAlignment:UITextAlignmentCenter];
[title setTextColor:[UIColor grayColor]];
[title.layer setShadowColor:[[UIColor colorWithWhite:1.0 alpha:0.5] CGColor]];
[title.layer setShadowOffset:CGSizeMake(0, 1)];
[title.layer setShadowRadius:0.0];
[title.layer setShadowOpacity:1.0];
[title.layer setMasksToBounds:NO];
[title setText:#"Sample Title"];
[title sizeToFit];
// [[[UIBarButtonItem alloc] initWithCustomView:title] autorelease]
Use a UIBarButtonItem in the plain style and additionally cover the toolbar in the appropriate area with a UIView that has a clear background. The view consumes the taps and hides them from the bar button item. Make sure you set the autoresizing mask correctly.
You can uncheck "Shows touch on Highlight" in Interface Builder.