Can you add a custom button to the Utilities Panel in Xcode? - ios

Just wondering if it's possible to add a custom button to the Utilities Panel in Xcode that you can drag it into a storyboard scene and have certain aspects already set, color, font/size, gradient etc.?

You are describing a custom xib file containing a view and its subviews. You then load the xib and stick the view in your interface in code, wherever and whenever you need it. Okay, you won't be able to see the effects of that within the storyboard, but it solves the problem extremely neatly.
Another possibility, if this is just about a button and nothing else, would be a UIButton subclass where the button configures itself to have the features you want.
But there is no way to drag-and-drop a custom button from the Object library into the design canvas. What's in the Object library is what's in the Object library and you can't change that.

Related

iOS add view to Button in IB

I am trying to add a view on a UIButton inside IB. The only problem it doesn't allow me to put in inside the button only on top?
Is this not possible through IB or am I doing it wrong?
It's not possible in Interface Builder. You have to add it in code.
You should not do this:
Do Not Customize Controls by Embedding Subviews
Although it is technically possible to add subviews to the standard system controls—objects that inherit from UIControl—you should never customize them in this way. Controls that support customizations do so through explicit and well-documented interfaces in the control class itself. For example, the UIButton class contains methods for setting the title and background images for the button. Using the defined customization points means that your code will always work correctly. Circumventing these methods, by embedding a custom image view or label inside the button, might cause your application to behave incorrectly now or at some point in the future if the button’s implementation changes.
https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/ViewPG_iPhoneOS/WindowsandViews/WindowsandViews.html#//apple_ref/doc/uid/TP40009503-CH2-SW26
If you need to add a UIView on your UIButton you can achieve it in 2 different ways
The easy way is to follow Cyrille answer: you can do it programmatically because IB doesn't allow you to modify a UIBUtton adding a view on it
The hard way is to create your custom button (let me call it "MYCustomButton"), that extends a UIButton, and use it in your application. With this way when you need to modify the buttons in your interface, you can achieve it modifying the XIB of the "MYCustomButton".

How do you stop Interface Builder embedding a control in a UIView when I drag it around?

When I move controls in Interface Builder and pass over a UIView. Is there anyway to stop IB from embedding the control in the UIView and making it a child of the UIView in the tree hierarchy.
BEFORE
+UIView
+UIButton
+UIView
If I move the UIButton with the mouse and place it above the UIView IB will make it a child of the UIView
AFTER
+UIView
+UIButton
+UIView
Is there anyway to lock the bottom view. I often use them as backgrounds and tint them.
You can reposition without embedding by pressing and holding Cmd while dragging the element.
Edit: This is only confirmed to work with Xcode 7.
I gave up and decided all groups of controls should have a parent UIView. Can be invisible (backgroundColor of clearColor)
Select all the controls then choose Editor menu > Embed in View.
After that we can safely move them around within their own grouping view without them jumping levels in the hierarchy.
One issue with Embed in View is when you use tagging and viewWithTag:
If you build your view using more than one VC controller and tag subviews make sure the tag ids are unique across the whole view hierarchy.
This is because viewWithTag: will only return the first control that matches the id and seems to search ACROSS the view hierachy before moving down a level to continue the search.
So if you choose Editor menu > Embed in View you're moving the control down a step in the hierarchy. So a call to viewWithTag:999 might have picked up your control before but now may return a completely different control.
It also may crash!
Its common to cast the result from UIView to control e.g UIImageView
Then call method on the control.
If viewWithTag find a different control than the one expected it may not even be a UIImageView so calling a missing method would throw exception.
BASICALLY never use tags as you have to debug in XIB and code and no checking for duplicate ids.
Drag from XIB to .h and create outlets instead.

How to connect custom UIView in interface builder using storyboards?

I want to use an F3BarGauge ( http://www.cocoacontrols.com/controls/f3bargauge ) in my iOS app and I want to use a .storyboard for my UI design.
I managed to achieve this programmatically by writing code into the view controller's loadView: method, which creates the instance of the F3BarGauge and adds it to the view via [self.view addSubview:myBarGauge]; .
However, now I can't see the F3BarGauge at all in the preview in Interface Builder and therefor I also can't adjust the size or position of the F3BarGauge from there. I can only do this in the code. Moreover, I think it is confusing for my colleagues if I create parts of the UI elements automatically through the .storyboard and some in my own code.
I would like to find a solution for both of these disadvantages.
It is not important for me to see the actual custom UI element's content (in this case the F3BarGauge) in Interface Builder. If the frame of the F3BarGauge's area is displayed in Interface Builder and I can move and resize it, then I'm happy with the solution.
I do not have an example for this approach, because the example from the author of the F3BarGauge uses a .xib file and not a .storyboard.
I'm relatively new to iOS programming and Xcode / Interface Builder. Maybe there is a better way to achieve what I want than how I imagine my solution.
I don't think there is a way to see the F3BarGauge in IB, but you can add a UIView as a subview of your main view in IB, size and position it as you like, and then in code, add the F3BarGauge to the subview.
After Edit: Actually, after looking at the author's demo project, you can see it in IB. Just copy and paste his view from the demo project into a view controller (after deleting the default view first).

Xcode custom control via storyboard

I want to use a custom control in my project, specifically a horizontal picker view I found on cocoacontrols.com (http://cocoacontrols.com/platforms/ios/controls/cppickerview). I've been able to include it into my project, load data and it works very nicely.
The pickerView is setted up programatically on viewDidLoad but I'd really like to be able to use it via storyboards because I'm a using static tableView. I tried to add a UIView, set the class to the PickerView class and then set up the outlet. I builds without errors or warnings but the picker view does not appear. It only shows a white rectangle.
Anyone with experience in this? Is it possible at all or should I keep it programatically?
Thanks in advance!
Well, that's normal. CPPickerView does not seem to implement initWithCoder:... I only see an initWithFrame: in the source code, which obviously means you can only instantiate that custom UIView from code. Or you can change CPPickerView's implementation to support what you want. It's open source.

How can I create a dynamic overlay for a UIButton?

My application has a few portions that have really big buttons (640x130, 230x150, etc.) What I need is to have a way to update different portions of the button, with different text. Initially, I assumed that in my code I could create various UILabels and then add them as subviews to my button. However, as soon as I try to add a UILabel to the button as a sub-view, my app crashes.
What is the easiest way to create an overlay for a button, that I can completely layout myself, without preventing button taps from being interested using overlay controls?
I imagine there are multiple ways to solve this problem. However, the best solution for my case should use the fewest lines of code (I have quite a few of these types of buttons) and I'd like to be able to continue using some form of configurable button within IB.
I'm not opposed to subclassing UIButton but, if I do, I would like to be able to use it in IB. I've never created a custom UIView for such a circumstance, so I'd need help defining that type of subclass so that it will work correctly in IB.
You need to add the subview to the containing view - not the button. To ensure that is doesn't interfere with button presses, be sure to set it to:
[myCustomTextOverlay setUserInteractionEnabled:NO];

Resources