UISegmentedControl, rightBarButtonItem and rotation issues - ios

I'm trying to recreate (to some extent) the up and down arrows presented on Mail.app on iOS 7 and iOS 8. There's not much to it. I just insert a UISegmentedControl into a UIBarButtonItem, like this (if you do know a better way of replicating that UI, please let me know):
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:#[[UIImage imageNamed:#"ArrowDown"], [UIImage imageNamed:#"ArrowUp"]]];
[segmentedControl addTarget:self action:#selector(segmentedAction:) forControlEvents:UIControlEventValueChanged];
[segmentedControl setBackgroundImage:[[UIImage alloc] init] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[segmentedControl setDividerImage:[[UIImage alloc] init] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[segmentedControl setFrame:CGRectMake(0, 0, 100, 30)];
[segmentedControl setMomentary:YES];
UIBarButtonItem *segmentedButton = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
self.navigationItem.rightBarButtonItem = segmentedButton;
Notice the calls to setBackgroundImage: and setDividerImage:. They are needed so the control looks just like the one from Mail.app. And that setup works:
Trouble is, it doesn't stay that way after a rotation. When using the iPhone in landscape, the rightBarButtonItem is automatically resized, looking broken and staying that way even after returning the device to portrait mode:
The control still works as planned , but well... It doesn't look so pretty anymore.
I have experimented with lots of properties and different setups, and nothing seems to work, except not setting an empty UIImage for the control background. If I comment out the setBackgroundImage: and the setDividerImage: pieces of code, the control keeps its look even after rotating:
But that's not the UI I have been looking for.
Do you know of any way to achieve the intended look and still have the correct behavior around? Or even what could be causing the resizing in the first place?

You may try to use two UIButton instead to UISegmentControl.
UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:customView];
self.navigationItem.rightBarButtonItem = rightItem;

Related

Intercept back navigation in iOS7

I want to stop somebody going back on my Navigation Controller if they haven't saved their changes. Most posts on SO discuss overriding the back button (with a variety of techniques to do this). However, iOS7 allows you to now swipe to go back in a navigationViewController...
I did see the UINavigationControllerDelegate which looks like the right type of delegate I'd want to implement but I see no way to cancel a navigation action. Any ideas how to do this?
Unfortunately nothing changed in iOS7, you still need to fake your back button if you want to put some check into it.
By the way it easier now since you don't need to fake the arrow button.
Edit:
to do that:
UIButton *backButton = [[UIButton alloc] initWithFrame: CGRectMake(0, 0, 44.0f, 30.0f)];
[backButton setImage:[UIImage imageNamed:#"back.png"] forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(popVC) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];

UIButtonBarItem looks wrong

I think this is going to be a stupid question, but I can't seem to find the answer. I have a few simple lines of code to put a button in the navigation bar:
UIBarButtonItem *cancelButton=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"button-cancel.png"] style:UIBarButtonItemStylePlain target:self action:#selector(cancelPressed:)];
UINavigationItem *item = [[UINavigationItem alloc] init];
item.leftBarButtonItem = cancelButton;
item.hidesBackButton = YES;
[self.navigationBar pushNavigationItem:item animated:NO];
This button works fine, but it looks like this:
Any thoughts?
You probably want to create the bar button item using a custom view, where the custom view is a UIButton:
UIImage *cancelImage = [UIImage imageNamed:#"button-cancel"];
UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
cancelButton.frame = (CGRect){CGPointZero, cancelImage.size);
[cancelButton setImage:cancelImage forState:UIControlStateNormal];
UIBarButtonItem *cancelBarButton = [[UIBarButtonItem alloc] initWithCustomView:cancelButton];
Set your button (cancelButton) size according to the size of the button-cancel.png.
stopButton.frame = CGRectMake ();
Instead, create a custom type UIButton with your image. Set the target and selector of the UIbutton to what you wish the bar button item to do. Then initialize the bar button item as follows:
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
Where button is your UIButton using the desired image.
UIBarButtonItem/initWithImage: is typically used for making iconic buttons - not buttons that have text in them.
If you just want to change how the common textual UIBarButtonItem looks, you just need to set the background image of your bar button item. This way you don't have to have images for each button that contain your button text.
Docs: - (void)setBackgroundImage:(UIImage *)backgroundImage forState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics
You can also set this app-wide by calling setBackgroundImage: on the UIBarButtonItem appearance proxy.
Lastly, note that you'll likely need to create a resizeable image to pass to setBackgroundImage. This will let your single image accomodate any button size. See UIImage/resizeableImageWithCapInsets:resizingMode: (iOS6) or UIImage/stretchableImageWithLeftCapWidth:topCapHeight: (pre iOS6)
You can certainly do what #Wain suggests but there are drawbacks. For one, your press-handler will no longer be sending a UIBarButtonItem as the 'sender'. That may not seem like much until you have a common handler that suddenly needs to determine if the sender is a UIBarButtonItem or a UIButton, or if you want to present a UIPopoverController against this BarButtonItem (but you only have the UIButton reference...)

Custom UIBarButtonItem back button

I need to customize UIBarButtonItem back button so it is displayed with a simple arrow icon (like in Facebook, Twitter etc). Do I need to create a custom UIBarButtonItem and manually add it to the nav bar or is there a way to do this through appearance API?
Thanks
You have to make your own and add it
You can do it like this (this is for a custom button using an image)
UIImage* image3 = [UIImage imageNamed:#"favorites.png"];
CGRect frameimg = CGRectMake(0, 0, image3.size.width, image3.size.height);
UIButton *someButton2 = [[UIButton alloc] initWithFrame:frameimg];
[someButton2 setBackgroundImage:image3 forState:UIControlStateNormal];
[someButton2 addTarget:self action:#selector(loadFavorites)
forControlEvents:UIControlEventTouchUpInside];
[someButton2 setShowsTouchWhenHighlighted:YES];
UIBarButtonItem *favoritesButton =[[UIBarButtonItem alloc] initWithCustomView:someButton2];
self.navigationItem.rightBarButtonItems =[NSArray arrayWithObjects:favoritesButton, nil];
Okay so it turns out that you can't actually use backBarButtonItem with a UIBarButtonItem created with a custom view. The best I can offer is you can create a template with an image and a empty background image, although it feels a bit like spacer.gif. I threw together a quick example to show what I meant about the view controller ordering too.
http://cl.ly/36091p0l0120

Remove UIBarButtonItem image

I have a UIBarButtonItem that I originally change the background image to. But I would like to be able to change the same UIBarButtonItem back to a default looking one (specifically a done button). Then again back to the way it was prior to (that shouldn't be a problem).
Here is how I change the appearance at first:
[menuButton setBackButtonBackgroundImage:[UIImage imageNamed:#"menuButton.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
Then back I was trying things like:
menuButton.style = UIBarButtonItemStyleDone;
menuButton.title = #"Done";
//the above didn't do anything, so I tried to make my own image to
//replace the first image. But below did't do anything either.
[menuButton setBackButtonBackgroundImage:[UIImage imageNamed:#"doneButton.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
Use setBackgroundImage:... instead of setBackButtonBackgroundImage:..., you're only setting the appearance of back buttons (in navigation bars).

Custom Button initWithImage displaying a default button behind the custom image

I am trying to make a button for my navigation bar with a custom image. When I run the following code, the button appears as it should, except you can see another wider button behind it, sticking out the sides. How do I get rid of that other button?
UIBarButtonItem *emailButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"emailBut"]
style:UIBarButtonSystemItemCompose
target:self
action:#selector(emailSheet)];
self.navigationItem.rightBarButtonItem = emailButton;
Hmm... UIBarButtonSystemItemCompose is a system icon, so I guess you are probably overlaying your icon on top of it. You should instead use UIBarButtonItemStylePlain (or other styles) for your style: argument.
Edit:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(...)];
[button setBackgroundImage:someImage];
[button addTarget:self action:#selector(something:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
[self.navigationItem setRightBarButtonItem:barButtonItem];
Ok, for some reason initWithImage only puts the image in the center of a default button. The fix was to initWithCustomView as iBlue suggested.
Another thing to note is that the barBackButton doesn't allow custom views, so I had to make that one a leftButton instead of a backButton, with my own go back method. I hope Apple makes this easier in the future.

Resources