frame altering image size - ios

I'm trying to add my settings button to my toolbar but the frame I have to set for it is altering the image dimensions. How do i set the frame without messing with the image itself. The image is stored in an asset catalog for #1x, #2x, and #3x.
-(void)viewWillAppear:(BOOL)animated{
//Toolbar buttons
UIView *buttonContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
buttonContainer.backgroundColor = [UIColor clearColor];
UIButton *button0 = [UIButton buttonWithType:UIButtonTypeCustom];
--[button0 setFrame:CGRectMake(0, 0, 44, 44)];-------------
[button0 setBackgroundImage:[UIImage imageNamed:#"settings-128(1).png"] forState:UIControlStateNormal];
[button0 addTarget:self action:#selector(button0Action:) forControlEvents:UIControlEventTouchUpInside];
[button0 setShowsTouchWhenHighlighted:YES];
[buttonContainer addSubview:button0];
self.navigationItem.titleView = buttonContainer;
}

You have a few different options. If you don't absolutely need to make your button a different size than its background image, then you can set the background image and then get a size that will not stretch that background image, to assign to the button's frame, like:
[button0 setBackgroundImage:[UIImage imageNamed:#"settings-128(1).png"] forState:UIControlStateNormal];
const CGSize button0Size = [button0 sizeThatFits:CGSizeZero];
[button0 setFrame:CGRectMake(0, 0, button0Size.width, button0Size.height)];
If you actually want to change the size of your background image without distortion (for example, you might want to elongate the background image by copying pixels in its center) you should look into the resizableImageWithCapInsets: methods provided by UIImage.
If for some reason you need button0 to have a particular frame and you want its background to be a different size, you can subclass UIButton and in your subclass explicitly implement:
- (CGRect)backgroundRectForBounds:(CGRect)bounds
and return a correctly sized frame for your background image regardless of the button's bounds.
And of course, if all else fails, you can pad your asset settings-128(1).png with clear pixels to make the image size match the desired frame size. I don't recommend that approach if you can avoid it since it will cause any future changes to the button's size to require careful changes to the image (and the 2x and 3x versions of the image).

Related

UIButton's size is different to the enclosed UIImageView object

I put some view the UIButton object and set the image to the button using setImage:forState: method.
the image size is 19x8.
And, called [button setTranslatesAutoresizingMaskIntoConstraints:NO] method to adjust Autolayout. The autolayout is only setting position of button.
then, I found out the button size is 19x22 and the enclosed image size is 19x8. The image position is center vertically.
Why the button is not same to the image?
How can I make them to same size?
I added the sample code.
UIButton *button = [[UIButton alloc] initWithFame:CGRectZero];
[button setImage:[UIImage imageNamed:#"buttonImage"] forState:UIControlStateNormal]];
[button setTranslatesAutoresizingMaskIntoConstraints:NO];
That's it.
If you want the button to be of same size as the image then,first get the size of the image.
CGFloat width = myImage.size.width;
CGFloat height = myImage.size.height;
CGRect myRect = CGSizeMake:(myButton.frame.size.x,myButton.frame.size.y,width,height); //x and y with respect with superview.
[myButton setImage:myImage forState:UIControlStateNormal];
myButton.frame = myRect;

UIButton image dimension different from original

I have a 40x40 button and in it I wanted to place an image of 20x20 dimension in the center. Here is what I did.
Set the content mode to center.(No scaling)
Assigned the image as the 'image' property of the button.
But when I run the program and check the image's dimension, it is different. Am I doing something wrong here?
In UIButton class, you've image and backgroundImage properties which have different behaviour when you set an image. You can use one of the 2 depending on what you want to do.
In my case If I just set the image property of the button,The image will be in the center.
Check these things:
1.Make sure you set the *image* ,not the *backgroundImage*.
2.Make sure the size of your image assets is smaller than the button's size.
3.Er...I just know two things...
Create the Custom UIButton
Create the UIImageView and add it as subview of custom UIButton.
Please refer the below code snippet, it will add the image in centre of button.
-(void)addImageInCenterForButton:(UIButton *)button
{
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"SettingIcon"]];
CGRect imgRect = button.bounds;
imgRect.size.width = 20;
imgRect.size.height = 20;
imgRect.origin.x = (button.bounds.size.width - imgRect.size.width) / 2;
imgRect.origin.y = (button.bounds.size.height - imgRect.size.height) / 2;
imgView.frame = imgRect;
[button addSubview:imgView];
}
try this i hope it helps..
my image is of 20x20 dimension,and if i change the size of button it looks same and its also in the center of a button..
UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40 ,40)];
[btn setImage:[UIImage imageNamed:#"ok"] forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor redColor]];
[btn setContentMode:UIViewContentModeCenter];
[self.view addSubview:btn];

iOS Custom Font "Floats" Too High

I'm creating a custom UIBarButtonItem, like so:
UIImage *originalImage = [UIImage imageNamed:#"button"];
UIImage *buttonImage = [originalImage stretchableImageWithLeftCapWidth:10 topCapHeight:5];
UIButton *toolbarB = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 65, 29)];
[toolbarB setTitle:title forState:UIControlStateNormal];
[toolbarB.titleLabel setFont:[UIFont fontWithName:kLatoBold size:17.0f]];
[toolbarB setBackgroundImage:buttonImage forState:UIControlStateNormal];
[toolbarB addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolbarB];
Depending on which of my custom fonts I use, after inserting my UIBarButtonItem into my controller's navigationItem, I get one of the results below.
Why does the button text in the second result "float" higher than it is supposed to? The first button looks great, but the second one positions the text in an unnatural fashion... Could it be a problem with my font?
Button Text Displaying Properly
Annoying, Floating Button Text that is Too High!
It happens for some fonts, I don't know why, but I can suggest you to use UIEdgeInsets to push your text down a bit. I guess you cannot fix your font, so;
Here is a link for a similar solution; Aligning text and image on UIButton with imageEdgeInsets and titleEdgeInsets

Set the button frame fit to the view

I want to display a UIButton in the header section of a UITableView. This is the code I have tried
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0, 285, 44)];
UIButton *headerViewButton = [UIButton buttonWithType:UIButtonTypeCustom];
headerViewButton.frame = CGRectMake(headerView.bounds.size.width * 7/5, headerView.bounds.size.height * 1/4, 320.0, 30.0);
headerViewButton.backgroundColor = [UIColor grayColor];
[headerViewButton setTitle:#"Import main list from other field >" forState:UIControlStateNormal];
[headerViewButton addTarget:self
action:#selector(buttonPressed:)
forControlEvents:UIControlEventTouchDown];
[headerView addSubview:headerViewButton];
The problem is I am having a hard time adjusting the button frame, so that it fits fine in the landscape mode as well as portrait mode. I am not very comfortable with using Interface Builder. How can I adjust the button frame correctly so that it fits the view.
I have tried using setAutoresizingMask
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0, 320, 44)];
UIButton *headerViewButton = [UIButton buttonWithType:UIButtonTypeCustom];
headerViewButton.frame = CGRectMake(headerView.bounds.size.width * 7/5, headerView.bounds.size.height * 1/4, 290, 30.0);
headerViewButton.backgroundColor = [UIColor grayColor];
[headerViewButton setTitle:#"Import main list from other field >" forState:UIControlStateNormal];
[headerViewButton addTarget:self
action:#selector(buttonPressed:)
forControlEvents:UIControlEventTouchDown];
[headerViewButton setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleWidth];
[headerView addSubview:headerViewButton];
As the app loads for the first time, the button is missing, but as I change the orientation, the button appears. Every time I try, its the same pattern. The first time the app loads, the button would be missing, and it appears on both orientations after that.
You need to set the button's autoresizingMask to appropriate values. The values depend on how you want the button's size and/or position to adjust as its parent view's size changes.
In the code you posted, it is odd that you are making the button wider than its parent view.
Try setting
[headerViewButton setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth];
This way it will be resized as the superview changes. (Works the same as setting autoresizing options in IB)

How to increase tapable (hitting) area of (custom Type) UIButton without increasing size of background image

is it possible to increase tapable area of UIButton without changing size of Button's background Image
I tried:
[shareButton setContentEdgeInsets:UIEdgeInsetsMake(top, left, bottom, right)];
&
[shareButton setImageEdgeInsets:UIEdgeInsetsMake(top, left, bottom, right)];
but none of these worked.
Any Suggestion please?
Make the UIButton of type buttonWithType:UIButtonTypeCustom and assign to it an image of a smaller size.
Do not set the image as the background image or it'll grow with the button. Set it as the main image instead.
For example if you want to set the tappable area to a 64x64 size and you want to show an image sized 32x32: the button size should be be 64x64 and the image size should be 32x32.
Programmatically:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
// use an image with the desired size (for example 32x32)
[button setImage: [UIImage imageNamed: #"buttonIcon.png"] forState: UIControlStateNormal];
// just set the frame of the button (64x64)
[button setFrame: CGRectMake(xPositionOfMyButton, yPositionOfMyButton, 64, 64)];
Interface Builder:
Subclass the superview of the UIButton, and override hitTest:withEvent:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
CGPoint buttonPoint = [self convertPoint:point toView:_button];
if ([_button pointInside:buttonPoint withEvent:event]) { // you may add your requirement here
return _button;
}
return [super hitTest:point withEvent:event];
}
Use the design approach you like (Interface Builder / Visual Format Language) together with Autolayout and layout the UIButton with the required size. Set title or image as content and use a transparent image with the size of the tapable area as background image.
_button = [UIButton buttonWithType:UIButtonTypeCustom];
[_button setImage:[UIImage imageNamed:#"contentImage"] forState:UIControlStateNormal];
[_button setBackgroundImage:[UIImage imageNamed:#"transparentImage"] forState:UIControlStateNormal];
Here a example with _button.layer.borderWidth = 1.

Resources