How to create custom View without subclassing from UIView - swift

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.

Related

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.

How do I make a custom UIView accessible?

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]];

when to split view and controller

i often do not split view and controller related things correctly.
should i always subclass a uiview if i want custom uibuttons and backgrounds in my app and add style related stuff in my view or should i handle this in my controller? or probably subclass a uibutton?
what about alignings? when should i subclass a uiview, add buttons to it and align then or handle this in my controller?
thanks for your hints!
please leave some comments if something is unclear.
Should i always subclass a uiview if I want custom uibuttons and
backgrounds in my app and add style related stuff in my view
If your need are basic, you coan just insert wanted elements as subviews of your view without having to subclass anything. Buttons and views have enough properties to handle those simple needs. But everything depends on what are those needs.
Should i handle this in my controller? or probably subclass a
uibutton?
Subclassing UIButton for UIButton behaviour is not a bad idea :-) But as said before, what do you need as special behaviour ? A special image : there is a property for that. A special reaction on events, manage it into the controller on IBActions. A UIButton with UFO behaviours, ok, let's subclass it.
What about alignings? when should i subclass a uiview, add buttons to
it and align then or handle this in my controller?
Hmmm... I guess never. This can (should) be done in the controller. The controller controls the whole UIItems. So if you want to align one item regarding another, do it into the controller. If you want to align special graphics or text that is displayed into the button, sublass it and manage that in the drawRect method.

iphone programming ?. Change UITableViewCellEditingStyle image?

I just need to know is it possible to change UITableViewCellEditingStyle image ?. If possible please tell me How to do that?
I don't see any delegate methods for changing the editing-style view.
You could change the editing button title with the delegate method -tableView:titleForDeleteConfirmationButtonForRowAtIndexPath:, however.
You could also override UITableViewCell and create a custom cell with UITableViewCellEditingStyleNone and a custom editing style behavior and appearance.
In case you're still interested you can actually get this to work by intercepting willTransitionToState and didTransitionToState in a subclass of UITableViewCell. The solution does involve "hacking" around in the cell view hierarchy. Look for the description of recursiveDescription on UIView on how to "dump" a hierarchy, but you will find a UIImageView of which you can change the .image property...
Fairly ugly hack, but does not actually use any private api.

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.