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

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.

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.

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.

Using AFOpenFlow: adding subviews not just images

I am using the AFOpenFlow library to generate a coverflow-like effect, but trying to add arbitrary UIViews to an AFOpenflowView, not just images. I am doing this by creating a subclass of of AFItemView, lets call it NewAFItemView, which represents the view I want to add. I also extended AFOpenFlowView, call it NewAFOpenFlowView and overrode the method
-(AFItemView *)coverForIndex:(int)coverIndex;
In the overridden method, I allocate an NewAFItemView object and return it as the cover view for that index. The NewAFItemView views appear correctly in the open flow but scrolling is immediately disabled. Switching back to allocating AFItemViews works but using NewAFItemViews does not scroll.
I have not overridden any of the touch events.
Any ideas on what might be happening?
Cheers.
why donĀ“t you use iCarousel?
https://github.com/nicklockwood/iCarousel
i use this, and it works perfect for uiviews =).

Cocoa-Touch: How to do layouting

I have a view (UIScrollView), which loads some data, and displays various things from it in various subviews. So I have approx 10 subviews (UIImageView, UILabel) and I need to place them programatically considering their unpredictable contents (i.e. different height/width for the UILabels depending on the text property).
From what I've read, there is no layout framework for Cocoa-touch.
What is the best way to do this?
From what I can tell, I should put the contents in the views, then start calculating coordinates based on their frames after calling their sizeToFit methods.
This approach is very error-prone. Is there really no other way?
You are right, there are no automatic layout managers. Subclassing UIScrollView and overriding layoutSubviews is probably the right way to implement your custom algorithm. You can then call setNeedsLayout to do the layout.
Layout in Cocoa is typically done with auto-resizing (using autoresizingMask). You start with your view at some hard-coded initial size, say 200x200; place your subviews onto this view and set the autoresizing flags accordingly. This view is then free to be resized to its actual size, as determined by its parent view/window. The process is the same whether you use Interface Builder or whether you do it programmatically.
If you need a vertical stack of views you can use a table view.
If you want more complicated layout you need to implement it yourself, by overriding layoutSubviews.
I'm not aware of any automatic layout managers or the like.
So, I think you'll have to calculate the desired positions and sizes and update the frames of your subviews manually.
EDIT: I found this question where Brad Larson points to an example of a custom layout manager. HTH
You can use Interface Builder to create a view and then drag and drop elements into it.

iphone interface overlay pass-through events

alt text http://www.davidhomes.net/question.gif
I'm farily new to iphone dev (<3 months on my free time) and I'm starting development of my second app.
From the image, I'm adding a number of UIViews as subviews to my main UIViewController.view, the number of Views to add varies based on user selectable data.
Each view contains several controls, a label, a UITextField and a Horizontal UIViewPicker.
For simplicity I put a (VERY ROUGH) mock-up here with only two buttons.
Because I want to improve the GUI, I want to overlay an UIViewImage as the top sub-views of the added UIView, something like in the image.
The question is on passing through the events to the objects below it. I've read somewhere that one way was to use clipping, but the actual shape is more complex than just an oval frame.
Somewhere else I read that one could add four UIImages, one at each border, which would let the events pass through this hole. Seems like a dirty solution to me (Although I'm sure it would work)
Any ideas about the best way to do this? Any links to a tutorial or recipe online?
Your help is appreciated
thanks
david
Have you looked at protocols? You can define protocols for your views or objects, and the users of that object (the subviews underneath for example) can implement the protocol, set itself as the objects delegate and when actions happen they will notified through the protocols. So for example
An AboveView will declare a protocol that declares methods when a certain gesture was senced by that view so something like
-(void)didMakeCircleGesture...
as a property the underneathview will have a delegate, so in your method that actually sence the gesture youll have at the end something like
[delegate didMakeCircleGesture];
in turn the delegate is the view underneath or something, and it will conform to the protocol defined by the AboveView, and as part of it it will have to declare the method didMakeCircleGesture, so as a result when one makes a circle gesture in the AboveView the underneath view that conformed to the protocol will be told of the event and it can take appropriate action