Vertical alignment of UIBarButtonItem is problematic on iPad - ios

I'm using a custom UIView as the titleView in a UINavigationBar. The UIView's only subview is a UIToolbar to which I've added two UIBarButtonItems. One contains an image (the twitter icon in the screenshot below) and the other contains title text.
On iPhone, this all looks great, but on iPad (using the same xib files and code) the vertical alignment is off. Here it is on iPhone:
And here it is on iPad (notice that the twitter icon isn't vertically aligned with the text in other button item):
The code I'm using to set the title is:
NSDictionary *textAttributes = #{NSFontAttributeName: [UIFont systemFontOfSize:17.f],
NSForegroundColorAttributeName: [UIColor whiteColor]};
[self.caseIdButtonItem setTitleTextAttributes:textAttributes
forState:UIControlStateNormal];
self.caseIdButtonItem.title = [NSString stringWithFormat:#"%#%#", NSLocalizedString(#"Case #", nil), self.caseId];
The above lines get called in my view controller's viewDidLoad.
How can I ensure that the bar button items are all aligned vertically in my UIToolbar?

I was able to fix this by explicitly setting the UIView's frame height to 44.

Related

Setting the UITextView Text to the Beginning of the UITextView

I am using a UITextView and assigning some text in my code but for some reason it always starts after 5-6 lines. The UITextView is an AttributedTextView or allows Attributed strings.
self.bodyTextView.selectedRange = NSMakeRange(0, 0);
self.bodyTextView.font = [UIFont fontWithName:#"Cochin" size:17];
self.bodyTextView.text = #"This is written by Joe. This is written by Moo. This is written by Qoo";
To quote this answer:
A text view is a scroll view. iOS 7 will add a content offset automatically to scroll views, as it assumes they will want to scroll up behind the nav bar and title bar.
To prevent this, override the new method on UIViewController, automaticallyAdjustsScrollViewInsets, and return NO.
It deals with the Navigation bar overlapping the UIViewController's view's frame. When that is set to no then the UIViewController does not adjust anything to avoid the top navigation bar.
Instead of subclassing and overloading, you can just set the property to no.
(UIViewController).automaticallyAdjustsScrollViewInsets = NO;

Uibutton font alignment issue when using font property on iOS

I have a problem when setting a uibutton property:
[self.btnName.titleLabel setFont:[UIFont fontWithName:#"gillsansstd" size:16.0]];
It will disturb the text alignment, text shows top of button.
Why does setFont disturb the text alignment of uibutton?
Following Code set center (horizontally) of Alignment of UIButton titile .
myButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
Other Way
[myButton.titleLabel setTextAlignment:UITextAlignmentCenter];
This is what you need.
[btnName.titleLabel setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
You can vertically align the text in a button.
Choose Edge Inset from xib file.

How to prevent UIBarButtonItem with custom attributes from changing size

I have a UIBarButtonItem in a UIToolbar at the top of my iPad app (iOS 5.1.) I have its width set to 65 in Interface Builder. It is of style 'bordered' and identifier 'custom.' The text label and tint changes when pressed:
[btnA setTitle:#"State A"];
[btnA setTintColor:[UIColor STATE_A_COL];
And so on, taking on various labels and colors. This worked fine, the button didn't resize even though the titles for the various states are quite different in length.
I then added this code to set the font, on startup:
UIFont * futura = [UIFont fontWithName:#"Futura" size:13];
NSDictionary *attributes = [NSDictionary dictionaryWithObject:futura
forKey:UITextAttributeFont];
[btnA setTitleTextAttributes:attributes forState:UIControlStateNormal];
Now, the button is sized to fit the width of the title it has on startup. It changes size as the titles change. How can I lock the size? I don't understand the interplay here; I thought all I'd done was change the title font attribute, not anything else about the button.
I have also tried explicitly setting the width property:
[btnA setWidth:65.0];
Again to no avail.
I got the UIBarButtonItem to stop resizing by using the possibleTitles property, to give a hint as to the desired maximum width.
[btnA setPossibleTitles:[NSSet setWithObjects:#"State A', #"B", #"Final state", nil]];
This worked but I can't explicitly set the width to the size I want, so I'm leaving the question open.

Change Nav Bar Title Font - DIFFERENT

As you can see in the picture below, my UIViewController IS NOT a UINavigationController, it's a common UIViewController. What I did is I put a UINavigationBar using interface builder and above it I put a UIImage. The problem is that I want to change the font of this UINavigationBar. Anyone would have a clue on how to do it?
Usually, with a common UINavigationController I use the following code:
// this will appear as the title in the navigation bar
self.label = [[UILabel alloc] initWithFrame:CGRectZero];
self.label.backgroundColor = [UIColor clearColor];
self.label.font = [UIFont fontWithName:#"Copperplate" size:22];
self.label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
self.label.textAlignment = UITextAlignmentCenter;
self.label.textColor = [UIColor whiteColor]; // change this color
self.label.text = [self.navigationItem title];
self.navigationItem.titleView = label;
[label sizeToFit];
Well it should work the same way. I think you just need an IBOutlet for the UINavigationBar, or only for the UINavigationItem (the title for your UINavigationBar) and that's it.
Storyboard Solution
There's nothing wrong with the answer above but a really simple way to do this is to select the Navigation Bar in the storyboard. Then change the Title Font in the attributes inspector.
Nota Bene
This technique is also really useful when you want to change the font
across an entire set of views whenever you are using a navigation
controller. (Just change it in one place). Xcode 7.1.1 has a couple of bugs. One of those requires that you toggle the Bar Tint from the default to another color (you can always reset it to the default if needed) in order to see the font change.
Custom Fonts
The above is currently not working when selecting a custom font (as of Xcode 7.1.1).
Please see the following SO Answer for a workaround if you need a
custom font. (tldr; add an outlet to a button or label, change the
custom font on that control, set that control as the
UINavigationItem.titleView).

iOS: Text is not displayed inside of a rotated UITextField

I have created a UITextField with Interface Builder. In viewDidLoad, I rotate the text field to match the landscape view we need:
name.transform = CGAffineTransformMakeRotation(-(M_PI/2));
Unfortunately, this does not bring the text with it. The text sits outside of the textfield, behind the background, as seen below.
Based on other questions here at StackOverflow, I have tried:
name.textAlignment = UITextAlignmentLeft;
name.textAlignment = UITextAlignmentRight;
and this additional function:
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
name.textAlignment = UITextAlignmentLeft;
name.textAlignment = UITextAlignmentRight;
}
Neither solution works.
-----UPDATE-----
I was able to just orient the nib for landscape rather than portrait, and that solved the problem. However, this seems like a bug. I'd assume a rotated UITextField should bring the text with it.
Why are you rotating the text field manually? Just let your UIViewController do its normal rotation behaviour (which transforms the view controller's main view) and all the subviews will be usable in landscape.

Resources