Why does the Title in my UIButton misalign vertically in iOS8? - ios

I have a UIButton created in a Storyboard. It has no special constraints / insets or anything. The only changes in code to it are :
[btn setBackgroundImage: [UIImage imageNamed:#"SomeImage"]];
[btn setTitle:#"Done"];
However, the button seems to be middled up whenever I use it in Autolayout :
The only way I could get it back was when I changed the background image to something else, like in :
So there seems to be something rather messed up with the Background image. However, I don't get why it doesn't work badly in pre-iOS8 then. Also, when I changed the insets :
self.closeButton.contentEdgeInsets = UIEdgeInsetsMake(-15.0, 0.0, 0.0, 0.0);
Then I got the content inside the Button again. But now, the iOS-7 button was misaligned. Can someone help me out over here?

This is an iOS bug, fixed in iOS 8.1

Related

ios - navigationItem.titleView blinks when updated

I have a UIView, holding a UIButton, set as my navigationItem.titleView. When I update the text for this button, it briefly blinks (disappearing and reappearing with the new text.)
Is there anyway to keep it visible when it's changed? So, if I change 2015-2016 it appears as if only the last digit is updated to 6?
Thanks
I solved my problem. This has more to do with UIButton than anything in the navigationBar's titleView.
If a UIButton is set as [yourButton buttonWithType:UIButtonTypeCustom], then it will not blink when it has been updated with [yourButton setTitle:#"title" forState:UIControlStateNormal];

AspectFill does not fill the UIImageView

I have a button that has contentMode set to UIViewContentModeScaleAspectFill. I set it like this:
self.itemImage.imageView.contentMode = UIViewContentModeScaleAspectFill;
I also set it to Mode - Aspect Fill in IB for the button, just in case. And when I set the image, I DON'T set is as a background image:
[self.itemImage setImage:image forState:UIControlStateNormal];
However, occasionally my image does not fill the button (which I want it to do), like in this case:
See grey spaces on each side. Occurs on both iOS 6 and 7.
Any ideas?
At long last, I managed to find the solution that works in Alfie's answer to this SO question.
In short, if your UIButton (or its hidden imageView) does not respond to contentMode, use this code:
self.button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
self.button.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;

How to change button size xcode?

My code to change button size when press:
[_btnClick setImage:image forState:UIControlStateNormal];
// oldPosition =CGPointMake(sender.bounds.origin.x, sender.bounds.origin.y);
_btnClick.frame = CGRectMake(oldPosition.x+20 , oldPosition.y+10, 500, 500);
but when i press button, just image change and button size not change to new size.
How i fix it. Thank' for help.
In a related question, an explanation is provided. The only thing that seems valid would be to do what the top answer says. Disable Auto layout

iOS Storyboard segmented control jumps when clicked on

I have a storyboard with an imageview, a segmented control and a textview. After appearing I make some adjustments depending on the device screen size:
- (void) viewDidAppear:(BOOL)animated
{
NSLog(#"didAp");
CGSize mainFrameSize=_mainView.frame.size;
[_imageSpace setFrame:CGRectMake(0, 0, mainFrameSize.width, mainFrameSize.height*4/5)];
[_metadadataControl setFrame:CGRectMake(0, _imageSpace.frame.size.height, mainFrameSize.width, 30)];
[_metadataTextView setFrame:CGRectMake(0, _imageSpace.frame.size.height+_metadadataControl.frame.size.height, mainFrameSize.width, mainFrameSize.height-(_imageSpace.frame.size.height+_metadadataControl.frame.size.height))];
}
When the app launched everything looks like it should, but if I click the segmented control it jumps some points down. The above method is just called once before I clicked the control and I don't have any other setFrame method calls. It seems like it just loads the storyboard again.
Thank you for your help
rdelmar was right. It was an issue with constraints contradicting my frame settings. Switched to doing everything with auto layout and constraints afterwards.

How to not stretch an image in UIButton

I'm trying to create a custom UITableViewCell programmatically and one of the subviews of this cell is going to be a button with an image in it (a simple image of a magnifying glass). However, I want the button's image to be centered and scaled proportionately down to fit in the button and NOT to be stretched to fill the entire button. Below is my code where self refers to the custom UITableViewCell which I am placing the button into.
self.myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.myButton setBackgroundImage:[UIImage imageNamed: #image_name_here"] forState:UIControlStateNormal];
self.myButton.frame = CGRectMake(...//something, something)
self.myButton.imageView.contentMode = UIViewContentModeCenter;
[self.contentView addSubview:self.mySearchHelpButton];
Right now the image stretches to fill the entire button rather than scaling proportionately so that it fits nicely.
I have also tried setting the contentMode to UIViewContentModeScaleAspectFill but this doesn't seem to change anything. In fact, none of the different contentModes seem to change anything.
Have you tried setImage instead of setBackgroundImage?
Make sure the button is a Custom UIButton
Just using setImage: did not work for me on iOS9.
But it worked combined with myButton.imageView.contentMode = UIViewContentMode.ScaleAspectFit;
In Swift...
button.imageView?.contentMode = . scaleAspectFit
Storyboard
Property image.
You must define specific property in Runtime Attributes of Identity inspector – imageView.contentMode, where you set value relatively to rawValue position of enum UIViewContentMode. 1 means scaleAspectFit.
And button's alignment, in Attributes inspector:
Swift 4.2
myButton.imageView!.contentMode = .scaleAspectFit
Swift earlier version
myButton.imageView!.contentMode = UIViewContentMode.scaleAspectFit
Swift 4.X and 5.X
button.imageView!.contentMode = .scaleAspectFit
button.contentVerticalAlignment = .fill
button.contentHorizontalAlignment = .fill
I think you guys are missing the obvious issue. The image is too large for the button and iOS then has to guess how you want to scale it. Make sure your images are the right size and you won't have this issue. Of course this is for static images and buttons that you know the size for up-front -- not dynamic content.

Resources