How to stop UIBarButtonItem text from truncating? - ios

I have a UIBarButtonItem in a Navigation Bar with the text title "Save". When I transition to a fullscreen UIPopoverController and then dismiss it, the text in my UIBarButtonItem gets truncated to "S..e". For all other segues and views I have no problem when returning.
I've tried manually changing the width and setting "possibleTitles" to include long words but I can't stop the truncation.
I'm using a custom font if that makes a difference.

Try to init your UIBarButtonItem with a custom view.
[[UIBarButtonItem alloc] initWithCustomView:yourView];
Just make sure your custom view has the right frame (e.g. for an UILabel ,wide enough to not truncate its content). Things should work fine.

Perhaps helpful, but a UIBarButtonItem with a custom view (ex: UILabel) that is inserted into a UIToolbar can take on the intrinsic size of its contents as long as translatesAutoresizingMaskIntoConstraints is set to false. I believe this may work for UINavigationBar too:
private let barButtonLabel: UIBarButtonItem = {
let label = UILabel(frame: .zero)
label.translatesAutoresizingMaskIntoConstraints = false
return UIBarButtonItem(customView: label)
}()

Related

pageControl in navigationBar not centered

i've added a pageControl to the titleView using the interface builder. The problem is that it is not centered at all. I could actually just do a lot of white spaces after the leftBarButton text, but it is not ideal. What would be the solution?
Instead of adding your pagecontroller from Storyboard try doing this
pageControl = [[UIPageControl alloc] init];
pageControl.frame = CGRectMake(x, y, xx, yy);
pageControl.numberOfPages = 2;
pageControl.currentPage = 0;
self.navigationItem.titleView = pageControl;
I came across this post while looking for an answer for the exact problem. I ended up solving it by adding UIPageControl inside an UIView and set the proper constraints to center itself inside the UIView. You can do this in storyboard easily and add this UIView to titleView. Hope this helps.
The title view is centered automatically (if it's possible), so it's probably being pushed to the left by the right bar button item. You should add a background color to your title view for debugging purposes, so you can see if this is true. If that's the problem, you need to make your titleView smaller so it doesn't run into either button.
The other possibility (which you could see if you had a background color) is that the page control is not centered in the titleView. If that's the problem, you need to add a centerX constraint to the page control.

UIBarButtonItem with custom view flexibly sized

Is it somehow possible to get a UIBarButtonItem with a custom view containing a UISlider to automatically resize similarly to a UIBarButtonItem using the system style UIBarButtonSystemItemFlexibleSpace?
For example (for the sake of brevity I've left out setting frames and adding the toolbar as a subview to an existing view)
UISlider *slider = [UISlider new];
slider.minimumValue = 0;
slider.maximumValue = 1;
slider.value = 0.5
UIBarButtonItem *sliderItem = [[UIBarButtonItem alloc] initWithCustomView:slider];
UIBarButtonItem *exampleItem = [[UIBarButtonItem alloc] initWithTitle:#"Example" style:UIBarButtonItemStylePlain target:nil action:nil];
UIToolbar *toolbar = [UIToolbar new];
toolbar.items = #[sliderItem, exampleItem];
The idea being the toolbar is the full width of the screen, exampleItem can be a variable width due to localisation of it's title, and slideItem resizes similarly to UIBarButtonSystemItemFlexibleSpace to take up the remaining space in the toolbar.
But the results are instead the slider stays taking up what ever frame you've given it (or the default/minimum frame if left out) and the next button just sits up against it.
I've tried the method mentioned in the following answer make a UIBarButtonItem with customView behave like Flexible-Space item by changing the auto resizing mask of the slider, but it appears not to work (the question is about using a UITextField not a UISlider which is probably why.)
I've also tried using another UIView containing the UISlider as the custom view to intercept any sizeThatFits calls, but it in fact never gets called as I suspected.

BackButton title disappering when navigationItem.title too long

All my buttons in a NavigationItem are set in code for a specific view in the viewHierachy. The title is set with a titleLabel to set minimumScaleFactor and contentCompression to it:
titleLabel.text = self.bookTitel;
titleLabel.minimumScaleFactor = 0.5;
[titleLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultLow
forAxis:UILayoutConstraintAxisHorizontal];
[titleLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultLow
forAxis:UILayoutConstraintAxisVertical];
self.navigationItem.titleView = titleLabel;
Now when the bookTitle is too long it is shortened with ..., but the backButtons title disappears too.
UIBarButtonItem doesn't respond to setContentCompression: so I can't set it to requiredPriority.
How can I prevent the backButton from not showing?
The following is an extract from the Overview section of the UINavigationItem class reference:
The navigation item must provide a title to display when the view
controller is topmost on the navigation stack. In addition, the item
may contain additional buttons to display on the right side of the
navigation bar. You can specify buttons and views to display on the
left side of the toolbar using the leftBarButtonItems property but the
navigation controller displays those buttons only if there is space
available.
If there isn't enough space, the navigation controller won't display any buttons on the left hand side, where your back button is. That's up to the navigation controller...
To prevent the backButton from not showing:
Can you restrict the size of titleLabel further? (The size of the view).

UIBarButtonItem - Make it not clickable when just text

So I have a UIToolbar with 4 UIBarButtonItems on it (2 of them being Flexible Space) the middle one is of Style: Plain with a Title set "Settings" and then the far right button is a 'Done' button to close the view.
Everything looks good and works the way I want except for the middle text button is "clickable", it does nothing when you click on it but there is a white glow that appears around the text once it's touched.
The only options you get with a UIBarButtonItem is 'Enabled' which if turned OFF makes the text greyed out.
I was able to almost simulate the way it looks by creating a UILabel and adding as a subview to the UIToolbar but I was wondering if there's a simpler way to doing this.
Thanks
use the customView of UIBarButtonItem, to set an UILAbel as customView. the item will not be clickable.
UILabel *yourLabel = ...;
UIBarButtonItem *theBarItem = [[UIBarButtonItem alloc] initWithCustomView:yourLabel];
that's all.

Turn off highlighting on UIBarButtonItem

I'm trying to use a UIBarButtonItem to put a title on my UIToolbar. I'm using the plain style and that looks fine, but I can't seem to get it to stop highlighting on touch. The Shows Touch When Highlighted option isn't available for the bar button items. Is there a quick and easy way to do this? I'm trying to do the building in interface builder so I can see what I'm doing. I'd prefer not to build the toolbar in the view did load every time.
The property responsible for this is accessible in the UIButton class:
myButton.showsTouchWhenHighlighted = NO;
You can access this (programmatically) in a UIBarButtonItem by assigning a UIButton to the bar button item's customView property, and configuring the button. You can do this in Interface Builder too: drag a UIButton onto a UIToolbar, and it will automatically embed it in a UIBarButtonItem for you - then look for the "Shows Touch On Highlight" checkbox under the button's settings.
Incidentally, I don't know how you're customising your buttons so feel free to ignore this, but if your button looks and behaves like a standard toolbar item then users will expect the glow effect.
I wanted a solution that could be used without any modification to my XIB structure.
The most obvious and simple one worked: subclass UIBarButtonItem:
UITitleBarButtonItem.h:
//
// UITitleBarButtonItem.m
// Created by Guillaume Cerquant - MacMation on 09/08/12.
//
/*
* A UIBarButtonItem that does not show any highlight on the touch
* Drag and drop a normal UIBarButtonItem in your xib and set its subclass to UITitleBarButtonItem
*/
#interface UITitleBarButtonItem : UIBarButtonItem
#end
UITitleBarButtonItem.m:
#import "UITitleBarButtonItem.h"
#implementation UITitleBarButtonItem
// Only caring about UITitleBarButtonItem set up in Interface Builder. Update this class if you need to instantiate it from code
- (void) awakeFromNib {
UIView *theView = [self valueForKey:#"view"];
if ([theView respondsToSelector:#selector(setUserInteractionEnabled:)]) {
theView.userInteractionEnabled = NO;
}
}
#end
Tested on iOS 5 and the one we aren't allowed to talk yet.
Alternative: 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.
My solution was to set it to disabled, and adjust the titleAttributes for each UIControlState
let attributes: [NSAttributedStringKey: Any] = [
.font: UIFont.boldSystemFont(ofSize: 16),
.foregroundColor: UIColor.white
]
barButton.setTitleTextAttributes(attributes, for: .enabled)
barButton.setTitleTextAttributes(attributes, for: .disabled)
barButton.isEnabled = false

Resources