Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 03-17-2009, 04:26 AM   #1 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: Colombo, Sri Lanka
Posts: 58
tharindufit is on a distinguished road
Send a message via Yahoo to tharindufit Send a message via Skype™ to tharindufit
Cool How to implement a custom ui view

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
tharindufit is offline   Reply With Quote
Old 03-17-2009, 04:44 AM   #2 (permalink)
Candy Mountain, Charlie..
 
ovidiu's Avatar
 
Join Date: Mar 2009
Location: Romania
Age: 26
Posts: 116
ovidiu is on a distinguished road
Default

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.
ovidiu is offline   Reply With Quote
Old 10-12-2010, 11:02 AM   #3 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 8
jowie is on a distinguished road
Question

Quote:
Originally Posted by ovidiu View Post
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?
jowie is offline   Reply With Quote
Old 10-12-2010, 01:03 PM   #4 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by tharindufit View Post
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

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 06-10-2011, 08:44 AM   #5 (permalink)
Registered Member
 
Join Date: Dec 2010
Posts: 3
DennisWChristopher is on a distinguished road
Default add PDF to custom view

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?
DennisWChristopher is offline   Reply With Quote
Reply

Bookmarks

Tags
custom, uiimageview, uilabel, uiview

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 340
3 members and 337 guests
guusleijsten, Kryckter, LEARN2MAKE
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,113
Posts: 402,880
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 08:48 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0