views:

3681

answers:

5

Hello.

I'm trying to find a way to replace the dots of a UIPageControl with a caption that reads "Page X of Y" since I'll likely have >50 items. I'm just becoming familiar with Cocoa, and I was wondering what is the best way to do this. Can I subclass UIPageControl? Should I use a custom view with labels? Or something else?

Thanks in advance. Any direction would be useful. --MG

+2  A: 

I don't think writing a custom page control will violate the the Human Interface Guidelines. I think showing > 50 dots would though ;)

I think it's perfectly acceptable to go both ways.

You could create a custom subclass of UIPageControl and override the default dot drawing behavior. You'd have to be careful not to override other behavior you do need.

Or, since the UIPageControl class isn't horribly complex to re-implement and writing a entire new class will make sure you don't override behavior you need when you override the dot drawing logic, you could just re-implement it. If you do, try to use Apple's naming of variables. This way everybody will immediately know how to use your class.

klaaspieter
+1  A: 

Although you can subclass UIPageControl, I don't think it will add anything. I suggest you subClass UIControll and base you API on UIPAgeControl's API. I think the UIs would be identical.

You could add properties to control the text but it's probably not needed.

Roger Nolan
+1  A: 

I ended up using this code by Matt Gallagher to achieve my paging view. It's also more efficient since it re-uses two views, instead of creating them all at once, as is done in the Apple sample code for PageControl.

Michael Grinich
+2  A: 

Just use a UILabel and set its text w/ a format string in your scroll delegate that says "Page N of K" or somesuch. Invisible left/right buttons can be added with simple transparent UIButtons.

Up to you if you really want to write the UIControl subclass yourself....

Andrew Pouliot
+2  A: 

Also, I should add for anyone considering subclassing UIPageControl (as I came here (unfortunately) after doing), the class for some boneheaded reason doesn't do its drawing in drawRect:, or any other public method for that matter. I suspect that at some point during page updates it sets up a drawing context and fills it with its dots, but I am here to warn you:

  • You cannot override drawRect: in UIPageControl so that you draw custom dots instead of the standard dots. Even if you get your draw rect to draw your dots, UIPageControl will always (yay!) draw its little white dots.

This makes the class essentially un-subclassable. Sure, you could, but why?

And more importantly, why would apple make a class with such ridiculously abnormal behavior?! drawRect: is there for a reason! This reminds me of working with NSMatrix back in the pre- imagebrowser days...

garg
No you're good, look here: http://www.onidev.com/2009/12/02/customisable-uipagecontrol/
garg