How do I make a custom UIView accessible? - iphone

I have a custom UIView subclass that contains a grid of cells, each of which are also custom UIView subclasses.
I'm interested in using the Keep It Functional test framework, which requires that every view have an acccessibilityLabel.
How do I configure the cell classes to have accessibility labels, so I can refer to them individually in my tests?

I think my autocomplete was lying to me. All I had to do was:
[gridCell setIsAccessibilityElement:YES];
[gridCell setAccessibilityLabel:[NSString stringWithFormat:#"cell-%d", cellIndex]];

Related

How to create custom View without subclassing from UIView

I'm hoping someone could help me.
I'm wondering if it was possible to create a custom View without subclassing from UIView. The reason being I wish to have just the bare minimum of functions that I need without all the other clutter that you find in UIView.
Is this possible or am I strictly bound to making all possible Subclasses I wish to create come from UIView? If this is the case is it possible for me to hide some of the functions and attributes in UIView from outside observers and only reveal what I need revealed?
I may be completely misinterpreting your question, but creating a subclass of UIView allows you to edit your view specifically to what you want with no required methods.
For example:
class CustomView: UIView {
//Your custom view code here
}
Can be implemented with complete customizability.
Perhaps you are referring to an extension of UIView? You can even use an extension of CustomView at this point. I guess it is very unclear what you are asking.
You can design the view in storyboard and have the code in viewController. Depends on how you want to design your program and if you want to reuse your custom view.

Re-using complex custom UI elements - Xcode Storyboard

In Xcode on the StoryBoard I built a complex UI element consisting of many sub-views.
I encapsulated it all into a UIView subclass but each element has to be built out on the storyboard wherever it is used.
Is there a way to build it in such a way that all a developer would need to do to use it is to add a UIView of the appropriate sub-class and not need to layout all of the subviews? Similar to how we do not need to interact with the underlying structure of buttons or labels.
This is possible, but it requires a bit of a workaround. It basically requires you to create a UIView subclass, which loads all it's subviews from a separate nib file
This question has the answers for you.

iOS Creating a list of tags

I'm currently trying to implement a feature in my app that shows tags for a post. I want it to work very similar to that of tags here on StackOverflow, in that they have a colored background.
Another example, are the Inline Labels here.
I'm just not quite sure on how to get it implemented. My first guess would to create an array of UILabels... Any suggestions?
Figure out what you want the tags to look like. If you can achieve that appearance with existing components like labels or tokens, then great, problem solved. If not, creating your own UIView subclass that draws a background and bit of text is pretty simple -- you wouldn't need to write much more code than a custom -drawRect: method, and even that should be easy. For example, if you wanted something that looks like the Twitter-ish inline labels, you could start with a resizable image and then draw your text on top.
Don't be afraid to create your own view classes... it's fun!
You probably need to write two classes.
The first (let's call it HorizontalLayoutView) will extend UIView. It will serve as the container view to hold all of the tags. It would override the layoutSubviews method to arrange the subviews by setting their frames. Create one instance of this and add it as a subview to your existing view.
The other (let's call that TagView) will also extend UIView, or perhaps UILabel. Instances of this class will represent each tag. Create one instance for each tag and add it as a subview to your horizontalLayoutView instance. In the initWithFrame: method, you would customize the tag to look the way you want. You can also override the drawRect: method to further customize its look.
If you are adding the tags dynamically after the view is already displayed, you may need to call setNeedsLayout on the horizontalLayoutView instance to get it to adjust properly.
Hope this will get you started in the right direction.

How to reuse common subelements of a custom UITableViewCell with iOS Interface Builder?

I have several custom UITableViewCell, which share a lot of common subelements.
I tried creating a UIView and using it as a subview for all the table cells, but obviously this wouldn't work, since entities in IB are object instances, not classes or templates.
How can I capture these common subelements in a custom control and drag it into all table cells? Ideally it would be a solution where one doesn't have to muck around with programmatic Nib loading and whatnot.
I think the "easiest" answer would be to create your CustomSubview class with the common, shared elements programmatically, and then in IB add a UIView subview and give it the class of CustomSubview. You'll have to write the subview in code, but you will still be able to reuse it in your various xibs.

Can't figure out where to start subclassing a UIControl!

I want to create my own control that will consist of several UILabels and a couple of UITextFields. The problem is I'm not sure where to start! Do I directly subclass UIControl, then create my subviews and add them to the main view in init:? Or do I use layoutSubviews? And will I need to override drawRect:?
I'm used to creating "Controller" classes that will handle adding subviews but if I subclass UIControl then I'm not sure what methods to override to set things up!
I've never done this before so I'd really appreciate a few pointers!
Cheers!
Are you sure you want UIControl? The UIControl class is intended for fairly simple, typically reusable controls like buttons and text fields that need to support a limited set of events (like "touched" or "value changed"). If you're just trying to create a way to group several views together, you should use UIView instead.
In either case, here's what you should do:
Create your subviews and set most of their properties in -initWithFrame:. Save them in instance variables and add them as subviews of self.
Set their frames in -layoutSubviews, calculating them based on self.bounds. This will be called any time your view changes size, including after -initWithFrame:.
You should not need to implement -drawRect: unless you need to do custom drawing with Core Graphics functions.