Jayyrus
Jayyrus

Reputation: 13051

How to resize background image as needed

i'm trying to create an ui button with image and text.

What i need is simple:

an image centered on the first row and on another row a label centered.

So i've created an uibutton in my storyboard, set text and background in this way:

enter image description here

But the result isn't good for me.

This is what i have:

enter image description here

and this is what i need:

enter image description here

Can someone help me?

thanks!

EDIT

I've created a custom UIButton in this way:

.h

#import <UIKit/UIKit.h>

@interface MenuButton : UIButton
@property (weak, nonatomic) IBOutlet UIImageView *image;
@property (weak, nonatomic) IBOutlet UILabel *text;
@end

.m

#import "MenuButton.h"

@implementation MenuButton

@end

And in interface builder i create a view with class MenuButton addedd an image and a label and connecting to .h.

Now my question is:

How can i pass to that new button the image and the label in order to instantiate multiple "custom button" with different value inside?

Thanks!

Upvotes: 0

Views: 51

Answers (1)

YYfim
YYfim

Reputation: 1412

You just create a UIButton subclass.

1)

If you are using IB: then you can create 2 properties in the .h

@property (weak, nonatomic) IBOutlet UIImageView *image;
@property (weak, nonatomic) IBOutlet UILabel *text

next you need to connect all the properties to the objects in the IB

If you are using code the remove the IBOutlet and move the properties to the .m

2)

Now all you need to do is manage the behaviour of the properties.

in IB, just add the autolayout constraints to the subclass and change their constant property value - check this tutorial

in code, you can do this by setting their frame with animation to make them larger/smaller

EDIT

Ok, after reading your update you need to change the subclass from UIButton to UIView.

This way you can add a UIView in your storyboard and add the UImageView & UILable. After doing so you can change the UIView's class to your subclass.

Then, click on the UIView and open the connections inspector. You will see the IBOutlets you have declared in th UIView subclass, you can just drag from the connections inspector to the UIImageView & UILabel to crete the connections

Upvotes: 1

Related Questions