iOS - make UIImage take the same size as UIButton - ios

I create a custom bar button using
+ (UIBarButtonItem *)barBtnItem:(id)target action:(SEL)action imageName:(NSString *)imageName frame:(CGRect)frame
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
[btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
[btn setFrame:frame];
UIBarButtonItem *barBtn = [[UIBarButtonItem alloc] initWithCustomView:btn];
return barBtn;
}
The frame is of size
CGRectMake(0, 0, 22, 22)
However, I think due to aspect ratio, my image gets smaller but is correctly centered in the UIButton
However, I want the image to become the same size as of UIButton.
Can I do that?
I used XCode 7 and tried its slicing feature. But I could not make it work.
If I could do it via code?
I also looked at UIEdgeinsets but I do not fully understand cap/insets concepts.

Set the content mode of the button to UIViewContentModeScaleToFill.
btn.imageView.contentMode = UIViewContentModeScaleToFill
btn.imageView.frame = btn.bounds

check after write below code :
btn.contentMode= UIViewContentModeScaleToFill;
btn.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
btn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
Still not work then try below line of code. Set background image of UIButton instead of image.
+ (UIBarButtonItem *)barBtnItem:(id)target action:(SEL)action imageName:(NSString *)imageName frame:(CGRect)frame
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
[btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
[btn setFrame:frame];
UIBarButtonItem *barBtn = [[UIBarButtonItem alloc] initWithCustomView:btn];
return barBtn;
}
if not work use below line of code :
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:imageName] style:UIBarButtonItemStylePlain target:self action:#selector(handleBack:)];

you can do it through AutoLayouts !!! set image Constraints from top , bottom , left , right equal to Button !!

Related

Custom UIBarButtonItem keep Aspect Ratio for Image but Space out Buttons

I am adding some custom image UIBarButtonItems to my navigation bar. If I do not mess with the item.width property, then my buttons are too close together, but if I space them out with the width property, then the button image is distorted. How can I maintain the aspect ratio of the button image but space out my custom items?
// Create custom map button
UIButton *mapButton = [UIButton buttonWithType:UIButtonTypeCustom];
[mapButton setImage:[UIImage imageNamed:#"map.png"] forState:UIControlStateNormal];
[mapButton addTarget:self action:#selector(dismissMapView:) forControlEvents:UIControlEventTouchUpInside];
[mapButton setFrame:CGRectMake(280, 25, 30, 30)];
UIBarButtonItem *button1 = [[UIBarButtonItem alloc] initWithCustomView:mapButton];
button1.width = (self.drawer.frame.size.width / 5.0f); // Space it out a bit
// Create custom list button
UIButton *listButton = [UIButton buttonWithType:UIButtonTypeCustom];
[listButton setImage:[UIImage imageNamed:#"list.png"] forState:UIControlStateNormal];
[listButton addTarget:self action:#selector(scrollToRow:) forControlEvents:UIControlEventTouchUpInside];
[listButton setFrame:CGRectMake(280, 25, 30, 30)];
UIBarButtonItem *button2 = [[UIBarButtonItem alloc] initWithCustomView:listButton];
button2.width = (self.drawer.frame.size.width / 5.0f); // Space it out a bit
// Add buttons to drawer
self.drawer.items = #[button1,button2];
Try this code for adding custom bar button in navigationbar.
UIImage* image3 = [UIImage imageNamed:#"compose_message_button.png"];
CGRect frameimg = CGRectMake(250, 9, 43,32);
UIButton *SettingButton = [[UIButton alloc] initWithFrame:frameimg];
[SettingButton setBackgroundImage:image3 forState:UIControlStateNormal];
[SettingButton addTarget:self action:#selector(BtnWriteMessage:)
forControlEvents:UIControlEventTouchUpInside];
[SettingButton setShowsTouchWhenHighlighted:YES];
UIBarButtonItem *mailbutton =[[UIBarButtonItem alloc] initWithCustomView:SettingButton];
self.navigationItem.rightBarButtonItem=mailbutton;
[SettingButton release];
i hope this code is useful for you.
You can create some explicit spacer items using UIBarButtonSystemItemFixedSpace.
Or you could create your bar buttons with custom views (initWithCustomView:) and then you can lay the content views out as you like.

Navigation Bar custom button

backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton addTarget:self action:#selector(gotoAmphorasViewController) forControlEvents:UIControlEventTouchUpInside];
[backButton setFrame:CGRectMake(0.0f,0.0f, 44,44)];
The problem i am facing is that though the button dimensions are44*44, wherever i tap anywhere around it, the the button action is fired.
Please try the bellow code : its working properly
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *buttonImage = [UIImage imageNamed:#"back.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[button addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customBarItem;
[customBarItem release];
}
-(void)back {
[self.navigationController popViewControllerAnimated:YES];
}
Its not a bug. It is a default behaviour. In iPhone, for navigation bar buttons the touch detection is little more expanded rather than its frame. Just have a look on any other application. Everywhere the button get fired if we tap nearer but outside its frame.
It's the intended behaviour, if you really want to limit the area of the touch, you can wrap the button inside a UIView:
UIView *buttonContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
[buttonContainer addSubview:button];
_barButton = [[UIBarButtonItem alloc] initWithCustomView:buttonContainer];

UINavigationItem rightButton position is different on iOS6 vs iOS7 and missing touches

I am trying to make my uinavigationbar to align the same way as in the iOS6 version of my app. What I am finding is that on the iOS7 the uinavigationbar items are pulled in a bit towards the title. Also for example if you are hitting just a litle right of the uinavigationitem.rightButton, the hits are not registered, however if you hit left of it for atleast 20px you can get the hits. Can anyone help me with moving that touch area to the right as well?
Here is the code that I used to setup the button:
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
rightButton.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
rightButton.imageEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
rightButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
rightButton.frame = view.bounds;
rightButton.bounds = CGRectMake(0,0,44,44);
[view addSubview:rightButton];
[rightButton setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
[rightButton setImage:[[UIImage imageNamed:imageName] imageWithOpacity:0.8] forState:UIControlStateHighlighted];
[rightButton addTarget:target action:sel forControlEvents:UIControlEventTouchUpInside];
navItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:view];
Thanks,
Rajan
Use below code
UIImage *faceImage1= [UIImage imageNamed:#"slideshowarrow"];
UIButton *face1 = [UIButton buttonWithType:UIButtonTypeCustom];
face1.bounds = CGRectMake( 10, 0, faceImage1.size.width, faceImage1.size.height );
[face1 addTarget:self action:#selector(Goto_nextView) forControlEvents:UIControlEventTouchUpInside];
[face1 setImage:faceImage1 forState:UIControlStateNormal];
UIBarButtonItem *backButton1 = [[UIBarButtonItem alloc] initWithCustomView:face1];
self.navigationItem.rightBarButtonItem = backButton1;

Can't add rightBarButton to UINavigationBar

I've subclassed UINavigationController to my pop & push needs and now I want to add a custom background image and some custom buttons to the UINavigationBar.
I've read that I can do it (since iOS 5) inside the appdelegate but I'd like to keep it inside the UINavigationController subclass for future use.
I've tried doing something like this:
-(void)viewDidLoad
{
[super viewDidLoad];
UIButton *rightbutton = [UIButton buttonWithType:UIButtonTypeCustom];
[rightbutton setFrame:CGRectMake(280, 0, 56, 39)];
[rightbutton setImage:[UIImage imageNamed:#"top.png"] forState:UIControlStateNormal];
[rightbutton addTarget:self action:#selector(popMePlease) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightBarButton=[[UIBarButtonItem alloc] initWithCustomView:rightbutton];
[[self navigationItem] setRightBarButtonItem:rightBarButton];
}
But I can't see the rightBarButton. I don't know if viewDidLoad is the place to put it in.
In your code button x position starts from 280 . It shoud be near to 0.
Replace following line
[rightbutton setFrame:CGRectMake(280, 0, 56, 39)];
with
[rightbutton setFrame:CGRectMake(0, 0, 56, 39)];
Try this it's working fine.
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame = CGRectMake(0, 0, 40, 40);
[myButton setBackgroundImage:[UIImage imageNamed:#"imTitleBG.png"] forState:UIControlStateNormal];
UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc]initWithCustomView:myButton];
self.navigationItem.rightBarButtonItem = rightBtn;
try this code
UIBarButtonItem *btn = [[UIBarButtonItem alloc]
initWithCustomView:myButton];
self.navigationItem.rightBarButtonItem=btn;
-(void)Action
{
// Do what you want here
}
try this code might be helpful to you...

How to have iOS set a gradient on UIBarButtonItem image?

just wondering, I customised the UIBarButtonItems the following way
+ (UIBarButtonItem *)createBarButtonItemWithTitle:(NSString *)t target:(id)tgt action:(SEL)a
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *buttonImage = [[UIImage imageNamed:#"blabla.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 10, 0, 10)];
CGRect buttonFrame = [button frame];
buttonFrame.size.width = 35;
buttonFrame.size.height = buttonImage.size.height;
[button setFrame:buttonFrame];
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[button addTarget:tgt action:a forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
return buttonItem ;
}
But now, I don't have the shading/glossy effect on the button background image....
Is there a way to have it done automatically, or I have to do it programatically ?
EDIT:
To totally customize the bar button in such a way you'd need to provide something other than the flat green image in "blabla.png".
A different way to handle this would be to create a bordered style button with your image. Something like:
UIBarButtonItem* barButton = [UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"blabla.png"] style:UIBarButtonItemStyleBordered target:tgt selector:a];
The UIBarButtonItemStyleBordered style gives it the 3D raised button-y look. You can tint its appearance to match your bar if desired.

Resources