Facebook-Messages-like Bar Button Items? - iphone

I just want to create those buttons at the bottom of the image attached saying "Messages", "Updates", "Sent".
Are these buttons ready-made UIKit buttons? if so what controls are they?
Thank you!
F.

This is a UISegmentedControl, but I don't think there is support for the badge (the red circle with the number inside). Check this SO question for a similar reply.
You can also have a look to the three20 library, as Facebook is based on that and there might be a class with exactly what you need.

That is UISegmentedControl .. check out the link for tutorial...
hAPPY iCODNG...

And here's the code for it, as found in the documentation under "Navigation Controllers" in the View Controller Programming Guide for iOS:
Listing 3-3 Configuring a toolbar with a centered segmented control
- (void)configureToolbarItems
{
UIBarButtonItem *flexibleSpaceButtonItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil action:nil];
// Create and configure the segmented control
UISegmentedControl *sortToggle = [[UISegmentedControl alloc]
initWithItems:[NSArray arrayWithObjects:#"Ascending",
#"Descending", nil]];
sortToggle.segmentedControlStyle = UISegmentedControlStyleBar;
sortToggle.selectedSegmentIndex = 0;
[sortToggle addTarget:self action:#selector(toggleSorting:)
forControlEvents:UIControlEventValueChanged];
// Create the bar button item for the segmented control
UIBarButtonItem *sortToggleButtonItem = [[UIBarButtonItem alloc]
initWithCustomView:sortToggle];
[sortToggle release];
// Set our toolbar items
self.toolbarItems = [NSArray arrayWithObjects:
flexibleSpaceButtonItem,
sortToggleButtonItem,
flexibleSpaceButtonItem,
nil];
[sortToggleButtonItem release];
[flexibleSpaceButtonItem release];
}
F.

Related

Textfield overlapping with UIView's text on keyboard

I am working on iOS application and in one of the view I have to load user comments as well as let them post comments. Everything looked fine until keyboard appears on user taps on text field to enter comments. The text on the UIView is overlapping with textfield as shown below.
Thank you for taking a look.
In iOS it is customary to add all view elements within a keyboard-enabled view into a UIScrollView. This way, when the keyboard slides up - the other elements slide up with it to avoid being obstructed.
Lucky for you, someone has implemented a UIScrollView that avoids the keyboard automatically, so all you need is to insert your view elements in to one of these and everything should work perfectly.
https://github.com/michaeltyson/TPKeyboardAvoiding
Assuming that the UITextField and UIButton("Post") move up when the user taps on the textField you should move everything else above it up an equal distance at the same time.
With more than just a little bit of text on the screen, you will have to go with UIScrollView. You can then use the [self.scrollView setContentOffset:CGPointMake(0, offset) animated:YES]; to move up (or down) your text in relation to the keyboard.
Use inputAccessoryView for your post button and UITextField.
Like this:
-(void)addAccessoryViewOnTextView
{
UIToolbar *keyboardHeaderView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 1024, 39)];
keyboardHeaderView.tintColor = [UIColor blackColor];
/* Here use your post button and textfield.
UIBarButtonItem *nextBtn= [[UIBarButtonItem alloc]initWithTitle:#"Next" style:UIBarButtonItemStyleDone target:self action:#selector(nextEvent:)];
UIBarButtonItem *prevBtn= [[UIBarButtonItem alloc]initWithTitle:#"Previous" style:UIBarButtonItemStyleDone target:self action:#selector(prevEvent:)];
UIBarButtonItem *doneBtn= [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneEvent:)];*/
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[keyboardHeaderView setItems:[NSArray arrayWithObjects: doneBtn,flexibleSpace,prevBtn, nextBtn, nil]];
[yourTextField setInputAccessoryView:keyboardHeaderView];
keyboardHeaderView = nil;
}
And call this method in viewDidLoad or anywhere your UITextField initializes.

iphone IOS Anyway to get Added Control in ToolBar to come to front

This is a bit of a hack, but trying to get a badge on a toolbaritem. Almost there, but it appears in the back, tried normal methods to get to front with no luck.
deletedCountBadge = [CustomBadge customBadgeWithString:#"0"];
deletedCountBadge.frame = CGRectMake(100,10,25,25);
UIView *view = (UIView *)[bottomToolBar.subviews objectAtIndex:0];
[view addSubview:deletedCountBadge];
Got it to work with a couple of different pointers on the net.
1) If you are moving the toolbar around at all (.frame=) make sure to set the items after the layout
2) Can use negative spacing items to get it to show up where you want.
UIBarButtonItem *deleteBadge = [BarButtonItemBadge barButtonItemBadge:#"1" insideColor:[UIColor blueColor]];
UIBarButtonItem *negativeSeperator = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
negativeSeperator.width = -30;
[bottomToolBarDictionaryButtonItems addObject:deleteBadge];
[bottomToolBarDictionaryButtonItems addObject:negativeSeperator];
// after any layout, of tool bar
[bottomToolBar setItems:bottomToolBarDictionaryButtonItems];

How can I use the pointy back button for my own buttons/navbar items?

As I'm sure you know, navbars inside a navigation controller stack get a back button that shaped sort of like an arrow with a pointy end on the left. I want to use this button image for my own uibuttons and navbar items, but it's missing from from the attribute inspector > bar item > image drop down menu. Where is this graphic located and how can I use it?
here's a screen of the button to which I refer:
If you want to do this programatically -
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithImage:yourImage style:UIBarButtonItemStyleBordered target:nil action:nil];
self.navigationItem.backBarButtonItem = button;
[button release];
You can use like below in xib-

Problems adding a UIBarButtonItem programmatically or editing one inserted via IB

I'm working on project that uses a tab bar at the bottom to flip through 5 sections of my app. One of these sections loads a map view which then spawns a web view. This was done with Round Rect Buttons at first, but they got in the way of the views, so I decided to switch to a navigation bar. In Interface Builder I inserted a navigation item and within it two Bar Button Items (left and right side). I've gotten the functionality for both button to work by control dragging from file's owner to fire the method associated with each button. This works fine and launches my web view, but what I would like to be able to do and have yet to figure out how to so far, is changing one of the buttons to a "done" or "back" button once in the web view. I've tried creating both buttons programmatically but neither would appear. This is the code I tried using to accomplish this...
UIBarButtonItem *updatePosition = [[UIBarButtonItem alloc] initWithTitle:#"Update Position"
style:UIBarButtonItemStylePlain target:self action:#selector(findMe)];
self.navigationItem.leftBarButtonItem = updatePosition;
[updatePosition release];
When I run the app, the button does not show up on the navigation item I inserted using Xcode. The only way I can get the buttons to appear is by inserting them with Interface Builder as I previously mentioned. But then I can not change the left button to a "back" or "done" button once I've brought up the new view. I tried running this code to change the style of the button on the UIBarButtonItem I inserted using IB with this code
self.navigationItem.leftBarButtonItem.style = UIBarButtonItemStyleDone;
But to no avail.
Here are some Utility methods to help you along the way:
+ (UIBarButtonItem *)setRightBarButtonItem:(SEL)action target:(id)sender withImage:(NSString *)imageName {
UIImage *buttonImage = [UIImage imageNamed:imageName];
UIBarButtonItem *buttonItem = [[[UIBarButtonItem alloc] initWithImage:buttonImage
style:UIBarButtonItemStyleBordered
target:sender
action:action] autorelease];
return buttonItem;
}
+ (UIBarButtonItem *)setRightBarButtonItem:(SEL)action target:(id)sender withBarButtonSystemItem:(UIBarButtonSystemItem)systemItem {
UIBarButtonItem *buttonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:systemItem
target:sender
action:action] autorelease];
return buttonItem;
}
Usage:
// Done button.
self.navigationItem.rightBarButtonItem = [Utility setRightBarButtonItem:#selector(doneTapped:) target:self withBarButtonSystemItem:UIBarButtonSystemItemDone];

Adding button to left of UISearchBar

I am tearing my hair out on this one. My client wants to add a button to the left of a search bar like the example below:
(source: erik.co.uk)
But I just can't figure out how to do it. Apple don't seem to provide any documented method for adding custom buttons to a UISearchBar, let alone to the left of the search bar.
I've tried hacking around in Interface Builder adding a UIToolbar with a button in it to the left but I cannot find any combination of styles where the two line up properly to give the impression that they are one. There is always what looks like one pixel difference in the vertical alignment as you can see from the picture below:
(source: erik.co.uk)
I've searched around and just can't find the answer, but as we can see from the screenshot it must be possible!
Thank you in advance for your help.
Erik
Use a navigation bar instead of a toolbar. Set the search bar to the navigation bar's title view.
In Interface Builder:
Result:
You can replace the Bookmark image instead, and adjust its offset if necessary.
For example:
[self.searchDisplayController.searchBar setImage:[UIImage imageNamed:#"plus2"] forSearchBarIcon:UISearchBarIconBookmark state:UIControlStateNormal];
[self.searchDisplayController.searchBar setPositionAdjustment:UIOffsetMake(-10, 0) forSearchBarIcon:UISearchBarIconBookmark];
Handle the button event in the delegate method:
- (void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar
This is how it looks:
The first solution is to use UINavigationBar instead of UIToolbar, as KennyTM noticed. But you may not be satisfied with Navigation bar, like in my case, when I need to use 3 buttons (Navigation bar is allow to use only 2 buttons) - see the left picture. This is how I did it:
Use Toolbar with 3 buttons and Flexible Space Bar Button Item in the place where search bar should be placed.
Put search bar on (not in) the toolbar. To do so in Interface Builder, do not drag & drop the search bar on the toolbar. Instead, put it somewhere nearby and then move it to place using the arrow keys on the keyboard (or by changing X & Y position in Interface Builder).
Search bar left black line under it (see the right picture). To hide it I put one additional view with the height 1px and a white background over it.
It looks a bit dirty for me, so if you have a better solution, let me know.
The easiest solution is to add your SearchBar in TOP of your Toolbar, (not in), I give you the best solution I use in my company eBuildy:
UIBarButtonItem *mySettingsButton = [[UIBarButtonItem alloc] initWithTitle:#"Settings" style:UIBarButtonItemStyleBordered target:self action:#selector(refresh)];
UIBarButtonItem *mySpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *myRefreshButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refresh)];
UIToolbar *myTopToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0,0,320,40)];
UISearchBar *mySearchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(70,1,220,40)];
[myTopToolbar setItems:[NSArray arrayWithObjects:mySettingsButton,mySpacer,myRefreshButton, nil] animated:NO];
[self.view addSubview:myTopToolbar];
[self.view addSubview:mySearchBar];
answering an old question here but i was struggling with this one myself recently and found some shortcomings with the other answers for the situation i was trying to address. here's what i did in a subclass of UISearchBar:
first add a UIButton property (here "selectButton"). then override the initWithFrame method and do something similar to the following:
-(id)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame])
{
self.selectButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.selectButton.contentEdgeInsets = (UIEdgeInsets){.left=4,.right=4};
[self.selectButton addTarget:self action:#selector(pressedButton:) forControlEvents:UIControlEventTouchUpInside];
self.selectButton.titleLabel.numberOfLines = 1;
self.selectButton.titleLabel.adjustsFontSizeToFitWidth = YES;
self.selectButton.titleLabel.lineBreakMode = UILineBreakModeClip;
[self addSubview:self.selectButton];
[self.selectButton setFrame:CGRectMake(5, 6, 60, 31)];
}
return self;
}
Now you want to override the layout subviews method to resize the searchbar to the appropriate width, depending on whether or not the cancel button is showing. That should look something like this:
-(void)layoutSubviews
{
[super layoutSubviews];
float cancelButtonWidth = 65.0;
UITextField *searchField = [self.subviews objectAtIndex:1];
if (self.showsCancelButton == YES)
[searchField setFrame:CGRectMake(70, 6, self.frame.size.width - 70 - cancelButtonWidth, 31)];
else
[searchField setFrame:CGRectMake(70, 6, self.frame.size.width - 70, 31)];
}
Note that in the above method I added a constant for the cancelButtonWidth. I tried adding code to get the width from [self cancelButton] but that seems only accessible at runtime and doesn't allow the project to compile. In any case this should be a good start for what you need
If you want a custom button on the right, taking place of the Cancel button, just use this code (valid for iOS 9 and up):
[self.searchBar setShowsCancelButton:YES];
[[UIBarButtonItem appearanceWhenContainedIn:[self.searchBar class], nil] setTitle:#""];
[[UIBarButtonItem appearanceWhenContainedIn:[self.searchBar class], nil] setImage:[UIImage imageNamed:#"search"]];