Is it possible to reposition a UIBarButtonItem that's in a UINavigationBar horizontally? I've seen the barButtonItem setBackgroundVerticalPositionAdjustment:forBarMetrics: method (but that's for vertical positioning), and I know how to do it if I make it a custom button, but how do I take a UIBarButtonItem with the style UIBarButtonItemStyleBordered and horizontally reposition it (that is, move it left or right)?
Thanks in advance!
You can create a fixed size button as a 'filler' and add that to beside your existing button:
UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
(Then set the width to your desired value)
Related
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.
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-
I want to put an icon by side of the title of my navigation bar. I'd prefer not to implement it as a custom titleView, because then i'll need to create a custom titleView for each controller i put on the stack (and I have pretty deep nesting). I'm adding currently as an UIImageView to a navigationBar. My problem is to calculate exactly this icon's horizontal position. It depends on the width of the back button, which has each time another title. How do I calculate this back button frame? Googling on it seems doesn't bring any reasonable results.
Thanks in advance
You could calculate the size of a label, with
your text in it and experiment with what the button will add on..
CGSize s = [label sizeWithFont:_font];
I ran into this same issue. My solution was to add the icon as one of the leftBarButtonItems, after a flexible space. This pushes it up against the title.
UIBarButtonItem *space = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease];
UIView *icon = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"titleIcon"]] autorelease];
UIBarButtonItem *iconItem = [[[UIBarButtonItem alloc] initWithCustomView:icon] autorelease];
NSArray *items = [NSArray arrayWithObjects: space, iconItem, nil];
self.navigationItem.leftBarButtonItems = items;
self.navigationItem.leftItemsSupplementBackButton = YES;
It's not quite the same as creating a custom titleView, however, in two ways: (1) the title text remains centered if possible, with the icon to its left, rather than centering the title and icon together; and (2) when you push a new view onto the stack, the icon does not slide to the left with the title; instead it fades out, like the other bar button items.
So, depending on your requirements, this solution may or may not work for you. But at least it avoids trying to match the standard title text attributes in a custom title view. (As far as I can discover, there is no automatic way to do that.)
I created a toolbar with a translucent black style like so:
UIToolbar *toolbar = [UIToolbar new];
toolbar.barStyle = UIBarStyleBlack;
toolbar.translucent = YES;
I created a button item for it:
UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithImage:nil
style:UIBarButtonItemStyleBordered
target:self
action:#selector(mySelector:)];
I noticed that the button when in normal state appears light grey and turns to full black only when I press on it. This seems to be the opposite of how it should function (e.g. Photo app). I'd like it do be black in normal state and lighter when pressed. What am I missing?
Change the barstyle to UIBarStyleBlackTranslucent, as dumb as it sounds, and I believe it returns it to normal.
I have that Navigation Controller and I'm adding 1 button by code this way:
UIBarButtonItem *configButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"config.png"] style:UIBarButtonItemStyleBordered target:self action:#selector(showConfigWindow)];
self.navigationItem.leftBarButtonItem = configButton;
It's working right but the icons are black instead of white!!
If I use this:
UIBarButtonItem *configButton = [[UIBarButtonItem alloc] initWithTitle:#"Settings" style:UIBarButtonItemStyleBordered target:self action:#selector(showConfigWindow)];
The text is showed properly in white.
The icons are ok cause I'm using them through interface builder and they show right.
UIBarButtonItem renders images in grayscale according to the images alpha value. So, if your image is fully opaque, then it will always render black, no matter what color the source image is, but if your image is transparent, it will render some shade of grey.
In order to render it fully white, or a color, you need to create a custom UIBarButtonItem. See Can I have a UIBarButtonItem with a colored image? for details