How to resize background image as needed - ios

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:
But the result isn't good for me.
This is what i have:
and this is what i need:
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!

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

Related

Deactivate NSLayoutConstrain from storyboard?

I want to set up a constrain in storyboard but deactivate it in Storyboard
for a while, until a user interaction happens. Is it possible?
Try setting constraint.active to NO, then set it back to YES to re-enable it.
If you want to disable it in the storyboard, go to the Identity inspector(), then click the + button under the User Defined Runtime Attributes header. Double-click the section of the highlighted row that says “keyPath”. Type in active, then uncheck the checkbox.
Yes it is possible.
Create an IBOutlet for that NSLayoutConstraint and connect it your mentioned contraint in Storyboard.
#property (nonatomic, strong) IBOutlet UIView *myView;
#property (nonatomic, strong) IBOutlet NSLayoutConstraint *myConstraint;
myConstraint is a constraint on myView
On viewDidLoad write this code: [_myView removeConstraint:_myConstraint];
Then, when a user interaction happens: [_myView addConstraint:_myConstraint];

IBInspectable UIView property

Can I make my UIView #property inspectable?
#interface Superview : UIView
#property (nonatomic, weak, readonly) IBInspectable UIButton *stopButton;
#property (nonatomic, weak, readonly) IBInspectable PKCircleProgressView *circleProgressView;
#end
I've created a designable view PKCircleProgressView, and I want it to be editable from the IB. Also I've created another designable view which contains PKCircleProgressView as subview, and I want it to be editable too.
Is there any way to edit circleProgressView's properties if I use Superview in the IB?
I've come out only with one idea, to create a common protocol for both views, and implement methods such as:
- (void)setProgress:(CGFloat)progress {
self.circleProgressView.progress = progress;
}
but it is not easy to do it with every property, especially if I want to create another View that contains my Superview.
IBInspectable does not support UIView or it's subtypes. It supports simple values.
If your custom progress view is IBDesignable why not set it's properties like radius and foreground color to be IBInspectable and in interface builder select the progress view and change the progress view's properties?
If your creating an IBDesignable superview you might be able to expose the inspectable properties of it's button and progress view using something like the Decorator pattern.
Apple's Documentation
No you cannot make UIView Inspectable. The usage of IBInspectable is only for configuring "key value" coded property of an instance in a NIB or storyboard.
Suppose, if UIView will be the key then what will be its value? There won't be value for UIView, so you cannot make UIView Inspectable.
But we can make UIView's attributes Inspectable such as:
UIView's backgroundColor, width, height, etc
Sample project: IBDesignable and IBInspectable

Displaying a User Tag with a UILabel and UIButton

In my app, I have a list of tags corresponding to a user that I would like to display. The tag should have a text field and a button on the right hand side to delete the tag (much like the tags on this site when asking a question!)
I only want part of the tag to be clickable, so I don't want to place a UILabel inside a UIButton, and it seems that adding a UIButton to a UILabel is bad practice. What is the best way to create this object?
I want something similar to a UITableViewCell, but not a UITableViewCell because it won't be displayed in a table view.
Just to create a UIView subclass, we could name it TagView, give UILabel and UIButton to display, and don't forget to create a delegate protocol to for action response.
Maybe it like this.
#protocol TagViewDelegate
#optional
- (void)didSelectedTagView:(TagView *)tagView;
- (void)didTagViewAccessoryButtonTapped:(TagView *)tagView;
#end
#interface TagView : UIView
{
UILabel *label;
UIButton *accessoryButton;
}
#property (weak, nonatomic) id delegate;
#property (readonly, nonatomic) UILabel *label;
#end
Then implement a UITapGestureRecognizer for label and touchUpInside event for button
- (void)initUIElement {
// give it a gestureRecognizer for label to response the label did select
UITapGestureRecognizer *labelTapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(didSelectedLabel:)];
[label addGestureRecognizer:labelTapGR];
[accessoryButton addTarget:self action:#selector(accessoryTapped:) forControlEvents:UIControlEventTouchUpOutside];
}
in the method didSelectedLabel: and accessoryTapped: , give response to tagView's delegate like this.
- (void)didSelectedLabel:(UITapGestureRecognizer *)gestureRecognizer
{
[self.delegate didSelectedTagView:self];
}
- (void)accessoryTapped:(id)sender
{
[self.delegate didTagViewAccessoryButtonTapped:self];
}
There is a library you may take a look.
DWTagList
Just like cabellicar123 said, use a UIView and place your UILabel and UIButton inside it. You have a couple of ways to do this.
First, you can make a XIB file with the main view being your UIView, and just drag into it a UIButton and a UILabel and position them the way you want, and then in code load the nib when you need it.
Second is to create this UIView programmatically, and use the addSubview method and add your UILabel and UIButton that way. Make sure you have defined these two subviews with initWithFrame if you are using this method, so that they are correctly placed WITHIN the UIView. What I mean by this is that the origin of these views should be relative to the top left corner of the UIView.

Reference to a Table in a ViewController

I want to change that separator line color, that one that separate the lines, in my UITableView. I have this code in my views.
self.tableview.separatorColor = [UIColor colorWithRed:115/255.0 green:83/255.0 blue:14/255.0 alpha:1];
I don't have any problems in my UITableViewControllers, but in my ViewController that has a TableView component I can't do this away.
Help please.
You need to have reference to your UITableView. You can achieve this by adding
#property (nonatomic, weak) IBOutlet UITableView *tableView;
To your .h class file and then add New Referencing Outlet in your Interface Builder.
If you don't know how to create referencing outlet take a look at this post:
xcode5 add new referencing outlet

how do i get the name of a component in IOS?

I just dragged a UIImageview onto a storyboard in xcode 5. I am coming from .NET where this would add an object of type UIImageView to the main form. Is this the way to think about it in IOS? Where do I click in the xcode IDE to get the name of the instance of UIImageView?
You dragged a UIImageView into a storyboard. You need to see which view controller this UIImageView object lives in and then you can connect that UIImageView to an IBOutlet, which is how your view controller (the implementation or code for which you're working on, which also happens to be a subclass of UIViewController) will interact with the UIImageView object.
You have to write a IBOutlet in the corresponding ViewController.h file like this.
#property (nonatomic , strong) IBOutlet UIImageView *myImageView ;
Then in your ViewController.m , use #synthesize after #implementation tag.
Then go back to the Interface Builder and connect the IBOutlet myImageView to your ImageView which you have dragged there. Hope it helps.

Resources