Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

How to implement a custom ui view

tharindufittharindufit Posts: 58Registered Users
Hi,
I want to implement a custom UIView with a UILabel and an ImageView, I think I need to implement two methods after extending UIView class, drawRect and initWithFrame:
But I am not clear on how to do this. I have tried adding UILabel and ImageView as subviews at my drawrect but It did not work. Can any one help me on this. I would like to have some example custom UIView class code, where I can study and understand.
Thanks.
Tharindu
Post edited by tharindufit on

Replies

  • ovidiuovidiu Posts: 116Registered Users
    Here you can find some sample codes provided by Apple:

    Sample Code

    Also, feel free to register on

    iPhone Dev Center
    for a better understanding of the concept.
    Junior iPhone Developer

    Mobile Touch Romania
  • jowiejowie Posts: 8New Users
    ovidiu;61360 said:
    Here you can find some sample codes provided by Apple:

    Sample Code
    Do you know which is the best project in the Apple Sample Code relating to custom UIViews?
  • Duncan CDuncan C Posts: 8,038Tutorial Authors, Registered Users
    tharindufit;61355 said:
    Hi,
    I want to implement a custom UIView with a UILabel and an ImageView, I think I need to implement two methods after extending UIView class, drawRect and initWithFrame:
    But I am not clear on how to do this. I have tried adding UILabel and ImageView as subviews at my drawrect but It did not work. Can any one help me on this. I would like to have some example custom UIView class code, where I can study and understand.
    Thanks.
    Tharindu
    If you want a view that includes a label and an image view, you can just create a UIView and add the label and the image view as subviews. Any objects you add as subviews will be drawn automatically.

    You can add subviews to a UIView either in Interface Builder or in code. Both approaches work. It's a little easier in IB, but not that difficult in code either. You would add the subviews when the view is created, not in it's -drawRect. (The drawRect method gets called every time the view needs to redraw itself, so if you add subviews in your drawRect method you would end up adding subviews over and over and over, until you run out of memory and crash.)

    It's actually unusual to subclass UIView such that you need to implement a custom drawRect method. Apple actually discourages it, since most of the view objects built into iOS are highly optimized for working with the graphics hardware, and drawing with code in -drawRect requires code that runs on the CPU.

    Note that if you have a custom subclass of UIView, that subclass can both do drawing in a custom -drawRect method, AND contain subviews that draw themselves automatically. The subviews will be drawn on top of the superview.

    As an example, I have a "boxed view" class I use sometimes that is a UIView that will draw a box around itself, either with square or rounded corners. I use this to create the grouping box look from Mac OS. I can create a bunch of labels, text views, buttons, etc, and put them inside one of my boxed views as subviews. The boxed view object draws a box around itself, and then other items take care of drawing themselves.
    Regards,

    Duncan C
    WareTo

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
  • DennisWChristopherDennisWChristopher Posts: 3New Users
    Duncan said: "Note that if you have a custom subclass of UIView, that subclass can both do drawing in a custom -drawRect method, AND contain subviews that draw themselves automatically. The subviews will be drawn on top of the superview."

    This idea is very helpful. I am trying to do this but also generate a PDF from the view. The only reliable way I have found to do this is to make the pdf setup and generation calls from within drawRect. I would prefer to use Duncan's method to render the text in the view, i.e. use UILabel etc rather than draw the text within the drawRect method. But how then to get the entire view into the PDF?
Sign In or Register to comment.