Separator between toolbar items in UIToolbar - ios

How to add a separator between buttons in a UIToolbar?
Sample image is shown in the below link

I did it with a custom view button, with a 1 pixel wide background:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 1, 44)];
label.backgroundColor = [UIColor whiteColor];
UIBarButtonItem *divider = [[UIBarButtonItem alloc] initWithCustomView:label];
// Add button to array of toolbar items
[items addObject:divider];
// Or set items directly:
//toolbar.items = [NSArray arrayWithObject:divider];
label.text = #"";

I can think of two ways :
(a) You could make them very thin toolbar buttons with user interaction disabled.
(b) Your other choice would be to implement your own toolbar. I'd try (a) first ;)

You can also make a toolbar button and set the Title = |

If yo want a clean, simple layout I would prefer a very thin image with this separator in it. Then you can add fix distances between your normal button, the separator image button (user interaction disabled) and the next button.

Related

How to shove a label in a tableview full of buttons?

I have a table view full of buttons. A label is outside the tableview serving as heading. I want to shove it inside the table view, on top of course, without changing it to button. In other words I want to move it inside the tableview, but on top and not as a button,as a heading.
The label in question says "Select all things that apply to your home"
For code please see Set title labels of buttons in table view from an array full of strings
For Screenshot pls visit http://i60.tinypic.com/2qmf4wl.png
How about this?
UIView *headerView;
UILabel *labelView;
labelView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
labelView.font = [UIFont fontWithName:#"Trebuchet MS" size:22.0];
labelView.backgroundColor = [UIColor clearColor];
labelView.text = #"* Your important note here.";
labelView.textColor = [UIColor blackColor];
[headerView addSubview:labelView];
yourTableView.tableHeaderView = headerView;
It's hard to add a label to a tableviewcontroller. Two options I can think of:
Make "Select all things that apply to your home" your navigation item title. (you can do this right in the storyboard)
Place a table view in a normal view controller and leave enough space on top for a label. (again, you can drag a normal table view into a view controller in a storyboard)
Hope it helps!
you can creat a subclass of UITableViewCell and custom the cell for the tabe

Remove back button text and center title in navigation bar

I've removed the text for UIBarButton in AppDelegate:
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -1000.f) forBarMetrics:UIBarMetricsDefault];
Which works like a charm:
As you can see, this doesn't align the navigation title at horizontal center. What is the best solution to accomplish this globally for all views.
PS: I am using Storyboard.
You can create your own custom titleView with a UILabel as follows:
UIView *titleView = [[UIView alloc]initWithFrame:CGRectMake(0,0,100,50)];
UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,0,100,50)];
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.text = #"PAKKELISTE";
[titleView addSubview:titleLabel];
self.navigationItem.titleView = titleView;
The details of the frames, text, alignment, etc. are just an example. The main idea though is that you set a custom UIView as the navigationItem's titleView.
It could also be an issue with your back button offset. Try this approach instead for removing the "back" text (I haven't tried this before, but I'm curious if it will work).
self.navigationController.navigationBar.topItem.title = #"";

2 UIBarButtonItem's using the same custom view only added once

I want to add a thin line between items in my UIToolBar so I'm creating a UIBarButtonItem with a custom view like so:
UILabel *separatorLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 1, 44)];
separatorLabel.backgroundColor = [UIColor colorWithRGB:0xe5edec];
UIBarButtonItem *separator = [[UIBarButtonItem alloc] initWithCustomView:separatorLabel];
Then I add my separator to the items array:
[items addObjectsFromArray:[NSMutableArray arrayWithObjects:someButton1, separator, somebutton2, separator, someButton3, nil]];
I thought this would add 2 separators to my toolbar but it only adds the one at the end. Why is this?
I can fix this by creating 2 duplicate labels and UIBarButtonItem's, but is there any other better way?
Any given instance of UIView can only appear in once in the view hierarchy. If you think about the APIs defined on UIView, this is fairly obvious. For example, UIView has a frame property which defines it's location in the superview. The frame property wouldn't make sense if the viewed appeared in two places.
So you need multiple instances. You can streamline your code by defining a method that creates separators:
- (UILabel *)newSeparator
{
UILabel *separatorLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 1, 44)];
separatorLabel.backgroundColor = [UIColor colorWithRGB:0xe5edec];
UIBarButtonItem *separator = [[UIBarButtonItem alloc] initWithCustomView:separatorLabel];
return separator;
}
And then you can add your items like this:
[items addObjectsFromArray:#[button1, [self newSeparator], button2, [self newSeparator]];
Also, you don't need to use UILabel if you're only displaying a background color. You can just use UIView.
Yes,you just created only one UIBarButtonItem object,so it showed one.
I think the better way is creating a UIBarButtonItem subclass with custom label,then create two objects of the subclass.
I hope my answer can help you.

Adding updating elements such as a level number and coin total to a navigation bar?

I have managed to change the navigation bar to a custom stretch image and turn the back button a different colour. I'm having trouble setting a level number and coin total in the navigation bar.
I figure there has to be any easier way then messing about with progress HUD's and such - which is all I have seen mention from my research.
I'm trying to achieve a similar look to that attached below - element wise not graphically.
Thank you for any help in advance.
Two solutions:
Make the entire navigation bar into a custom view with your own elements, then update them as needed. Do this using the titleView property of UINavigationItem.
Use the leftBarButtonItem, titleView, and rightBarButtonItem with your own custom views.
I prefer the second method because it is more scalable (visually) - that is your view controller will layout correctly in landscape mode, or on an iPad, or in an oddly sized popover, etc. The left item will align to the left, the right to the right and the middle one in the middle. However, it's a bit more complicated because the left and right items need to be of type UIBarButtonItem. We can get around that like this:
// Set up the red star thing in the middle with a number 6 on it
MyRedStarView* redStarView = [MyRedStarView redStarViewWithValue:6];
self.navigationItem.titleView = redStarView;
// Set up the 'word' button on the left
MyWordButton* wordButton = [MyWordButton defaultWordButton];
UIView* buttonHolderView = [[UIView alloc] initWithFrame:CGRectZero];
buttonHolderView.backgroundColor = [UIColor clearColor];
[buttonHolderView addSubview:wordButton];
buttonHolderView.frame = wordButton.frame;
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:buttonHolderView];
// Set up the coin indicator on the right
MyCoinView* coinView = [MyCoinView coinViewWithValue:515];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:coinView];
Notice how I wrapped the button (on the leftBarButtonItem) in a holder view - this is only necessary if you want to do some kind of view transitions on the button. For example if it changes in different contexts and you want to animate the transition by removing the button from the holder view and adding a different one (with a view transition). On the right bar button item I didn't do this, just to show the different approaches.
Of course I used some fake view types to demonstrate - you'd actually have your own references to these so that you can set the property values and update the display of the numbers.
try the following code assuming you already have the navigationView in place
- (void)viewDidLoad
{
// Custom Navigation Bar
// -----------------------------------
UIView *navigationCustomTitle = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 200.0, 20.0)];
navigationCustomTitle.backgroundColor = [UIColor clearColor];
UIImageView *icon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"icon.png"]];
UILabel *titleCustomLabel = [[UILabel alloc] initWithFrame:CGRectMake(30.0, 0.0, 260.0, 20.0)];
titleCustomLabel.text = #"A nice title";
titleCustomLabel.textColor = [UIColor whiteColor];
titleCustomLabel.backgroundColor = [UIColor clearColor];
titleCustomLabel.font = [UIFont fontWithName:#"HelveticaNeue-CondensedBold" size:(16.0)];
[navigationCustomTitle addSubview:titleCustomLabel];
[navigationCustomTitle addSubview:icon];
self.navigationItem.titleView = navigationCustomTitle;
}

How can I have a space-filling segmented control in the toolbar in an iOS app?

I have a segmented control. Whenever the view has finished appearing I create a bar button item to hold it and set that as the toolbars item. The problem I'm having is that the segmented control will not fill up the space in the toolbar, even though it is set to have space-filling behaviour.
How can I have a space-filling segmented control in a toolbar in an iOS app?
It sounds like you're trying to get non-standard toolbar behavior out of a UIToolbar. Why not just drop a UIView in there and fill it with a UISegmentedControl in the normal way? Is there some specific functionality of the UIToolbar that you need?
There is no "space-filling behaviour" for UIViews in general. They get the size they are assigned. All you can do is:
set their autoresizing mask to control how they are resized if their
parent view changes its size
set their UIViewContentMode to control how they resize their content
(important for UIImageViews, for example)
In your case, you could do the following to get a UIToolbar containing a UISegmentedControl that is as wide as the toolbar:
(void)viewDidLoad
{
[super viewDidLoad];
// Create the toolbar; place it at the bottom of the view.
UIToolbar *myToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height-44, self.view.bounds.size.width, 44)];
myToolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
[self.view addSubview:myToolbar];
// Create the UISegmentedControl with two segments and "Bar" style. Set frame size to that of the toolbar minus 6pt margin on both sides, since 6pt is the padding that is enforced anyway by the UIToolbar.
UISegmentedControl *mySegmentedControl = [[UISegmentedControl alloc] initWithFrame:CGRectInset(myToolbar.frame, 6, 6)];
// Set autoresizing of the UISegmentedControl to stretch horizontally.
mySegmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
[mySegmentedControl insertSegmentWithTitle:#"First" atIndex:0 animated:NO];
[mySegmentedControl insertSegmentWithTitle:#"Second" atIndex:1 animated:NO];
mySegmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
// Create UIBarButtonItem with the UISegmentedControl as custom view, and add it to the toolbar's items
UIBarButtonItem *myBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:mySegmentedControl];
myToolbar.items = [NSArray arrayWithObject:myBarButtonItem];
}

Resources