IBInspectable UIView property - ios

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

Related

Buttons/Links under view object in .xib file are not responding

Can somebody tell me why none of the buttons under the "License Agreement View" UIView object in my .xib file are triggering an action? It seems that the Scrollview object is messing up the behavior. When the buttons are directly under the "Scroll View" object (as opposed to the "License Agreement View" UIView object), then they function properly. But, I need to group my buttons under the UIViews as shown in the view hierarchy below.
Here's the view layout:
Here's my view hierarchy:
Here's the corresponding .m file:
#interface MYViewController ()
- (IBAction)licenseAgreementPressed:(id)sender;
- (IBAction)legalDisclaimerPressed:(id)sender;
- (IBAction)privacyStatementPressed:(id)sender;
#property (strong, nonatomic) IBOutlet UIView *licenseAgreementView;
#property (weak, nonatomic) IBOutlet UIButton *legalDisclaimerButton;
#property (strong, nonatomic) IBOutlet UIButton *privacyStatementButton;
#end
#implementation MYViewController
- (IBAction)licenseAgreementPressed:(id)sender
{
NSLog(#"Pressed A");
}
- (IBAction)legalDisclaimerPressed:(id)sender
{
NSLog(#"Pressed B");
}
- (IBAction)privacyStatementPressed:(id)sender
{
NSLog(#"Pressed C");
}
#end
Make sure you have made connection between IBAction(code) and UIButton(in xib) , please check this post How do I name and link an IBAction button created in a storyboard
Did you connect your actions to the elements in interface builder by control-dragging it?
If your actions are connected you will see a little filled out circle on the left of the line in your editor.
Check this tutorial for further information on how to connect your storyboard elements to your code.
It is because none of the buttons are inside the bounds of the license agreement view. A button outside its superview's bounds is untappable.
So the problem was the links, buttons, and text fields were not actually located within the bounds specified the parent views because the original .xib file didn't have the required constraints pinned to those elements. I discovered this by checking the "clip to bounds" checkbox in the Attributes Inspector pane. Whenever "clip to bounds" was checked, the app was NOT displaying any of the above view objects; I could only see links, buttons, and text fields when "clip to bounds" was unchecked but unfortunately, those elements were not clickable at that point. After setting the necessary constraints, however, the view elements were correctly placed within the bounds of their parent views and the text fields, labels, and buttons became clickable. I'm attaching the constraints that I used to fix the problem. Note: the constraints that are not expanded only include a height constraint.

How to create a custom button design with labels inside it in Xcode 6 using Swift

I am learning Swift by creating a small quiz app. I need to create a button with few labels (as shown) for the level. Please guide on how to create such type of button.
The Level number and progress value is dynamic i.e. will be provided programmatically. Is there a way by which I can create a custom button assign it to a class with outlets for labels, so that I can create an object of the class and assign the values of the label and the entire class behaving as a button i.e. when clicked will move to another view controller showing the questions of that level.
You basically answered your own question.
Create a subclass of UIView like so (showing only header file):
#interface MyButton : UIView
#property (nonatomic, weak) IBOutlet UILabel *levelLabel;
#property (nonatomic, weak) IBOutlet UILabel *progressLabel;
#end
Now create a layout file (.xib) with one uiview and uilabels as its children. Set the uiviews class to MyButton and hook up the outlets to the two labels. Remember to set the userInteractionEnabled property of the view to YES.
Use your custom class anywhere in your app by importing "MyButton.h".
EDIT:
In Swift:
class MyButton: UIView {
#IBOutlet weak var lebelLabel: UILabel?
#IBOutlet weak var progressLabel: UILabel?
...
}
Regarding comment:
I created the xib along with labels and their outlets. Can you please tell how to use and initialise MyButton anywhere.
This has been answered on SO countless times before. For one way of doing it, read here

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:
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

How to use a custom UIButton category with Interface Builder?

I'm coding a simple card game for my younger sister on a whim. I'm using UIButtons: default state is face down, selected state is face up. I need the buttons to have a boolean property that tells me if they've ever been flipped over. If not, it gets set to true (that way I'm not just drawing random cards on each flip). I tried creating a category of UIButton called CardGameButton. In the .h file:
#interface UIButton (CardGameButton)
#property (nonatomic) BOOL discovered;
#end
In the .m file:
#implementation UIButton (CardGameButton)
#dynamic discovered;
#end
This is really all I need. How do I use this in IB? I have a bunch of UIButtons on a screen that I want to be CardGameButtons. But when I try to switch my calls to UIButton in my view controller to CardGameButton, it tells me CardGameButton isn't a type (yes I imported the file). And when I try to switch the class of the UIButtons in the storyboard to CardGameButtons, the console tells me that they're an "unknown class". I tried subclassing UIButton, but that didn't work (I just need them to be RoundedRectButtons with the extra property, and since you can't subclass RoundedRectButton they wouldn't display properly). How do I make this work in IB?
You're treating CardGameButtons as though it's a subclass of UIButton when instead it's a category. Anything you declare as UIButton will have the discovered property as standard, but you can't 'create' a CardGameButtons as it's not a thing in itself.
Categories are not types. They're just used to add some extended behavior to the class.
I suggest that you try subclassing UIButton and call it CardGameButton. Add the BOOL property to its interface. You can still make it a round rect button, by setting buttonType to UIButtonTypeRoundedRect "round rect buttons aren't a separate subclass.". Then override
- (void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event {
if(event.type == UIEventTypeTouches){
self.discovered = YES; // or handle the logic as you want here
}
[super sendAction:action to:target forEvent:event];
}
You can add a UIButton to your view from the IB, and then its class to your created subclass and set its type to round rect.
Edit: As you stated, buttonType is actually readOnly, so we can't use that to draw a round rect button. However, it turns out that it's not hard to draw it.
Check this tutorial. It shows how to do it using CALayer property of UIButton and using UIBezeirPath.

When will a control on a storyboard page be available to get frame info?

I'm using iOS 6 with storyboard and ARC.
I've got on my storyboard a UIView that I want to use for positioning and sizing - I've created an IBOutlet for the control in my controller via the Ctrl drag/drop so I know it's connected.
Now I want to try to reference this UIView from within the controller so I can dynamically create subviews within that UIView, positioned appropriately.
I'm trying to access the UIView from within viewDidLoad as follows
CGRect rect = self.myView.frame;
...but the rect is always coming up as 0, 0, 0, 0. myView is defined in the header file as:
#property (weak, nonatomic) IBOutlet UIView *myView;
I thought that all the controls from the storyboard would already be initialized when you got to viewDidLoad. If this is not the case, where should I put the code to use the frame of the UIView to create my subViews?
Thanks in advance.

Resources