Changing the color of UIPageControl dots in MonoTouch

I was wondering if MonoTouch would be able for the developer to change the color of the UIPageControl dots according to the light background - they appear to be fixed white, which makes them very difficult to read against a light textured background.

I know there is no public API for this, but I was wondering if something was internally implemented in MonoTouch to improve this.

Otherwise, what is the recommended approach for using UIPageControl on a light background?

+4
source share
3 answers

I took a hit to translate this. I'm not sure if this will work, but it will compile. Please note that the linked page contains comments indicating that Apple is frowning with this code and may reject it:

using System; using MonoTouch.Foundation; using MonoTouch.UIKit; namespace Whatever { public class StyledPageControl : UIPageControl { public StyledPageControl () : base() { } public override int CurrentPage { get { return base.CurrentPage; } set { base.CurrentPage = value; string imgActive = NSBundle.MainBundle.PathForResource("activeImage", "png"); string imgInactive = NSBundle.MainBundle.PathForResource("inactiveImage", "png"); for (int subviewIndex = 0; subviewIndex < this.Subviews.Length; subviewIndex++) { UIImageView subview = this.Subviews[subviewIndex] as UIImageView; if (subviewIndex == value) subview.Image = UIImage.FromFile(imgActive); else subview.Image = UIImage.FromFile(imgInactive); } } } public override int Pages { get { return base.Pages; } set { base.Pages = value; string img = NSBundle.MainBundle.PathForResource("inactiveImage", "png"); for (int subviewIndex = 0; subviewIndex < this.Subviews.Length; subviewIndex++) { UIImageView subview = this.Subviews[subviewIndex] as UIImageView; subview.Image = UIImage.FromFile(img); } } } } } 
+2
source

I combined this and this for MonoTouch. Hope this helps.

The use is as follows:

 _pager.Change += delegate(object sender, EventArgs e) { var pc = sender as PageControl; Console.WriteLine ("Change Delegate== " + pc.currentPage); var toPage = pc.currentPage; var pageOffset = _scroll.Frame.Width*toPage; PointF p = new PointF(pageOffset, 0); Console.WriteLine (pageOffset); _scroll.SetContentOffset(p,true); }; 

And a class like that.

 public class PageControl:UIView { #region ctor public PageControl (RectangleF rect) :base(rect) { this.BackgroundColor = UIColor.Red; this.CurrenColor=new CGColor(.2f,15f,10F); this.OtherColor =new CGColor(.77F,.71F,.60F); } #endregion #region Fields float kDotDiameter= 7.0f; float kDotSpacer = 7.0f; int _currentPage; int _numberOfPages; CGColor CurrenColor{get;set;} CGColor OtherColor{get;set;} public int currentPage { set { _currentPage = Math.Min(Math.Max(0, value),_numberOfPages-1); this.SetNeedsDisplay(); } get{return _currentPage;} } public int numberOfPages { set { _numberOfPages = Math.Max(0,value); _currentPage = Math.Min(Math.Max(0, _currentPage), _numberOfPages-1); this.SetNeedsDisplay(); } get{return _numberOfPages;} } #endregion #region Overrides public override void Draw (RectangleF rect) { base.Draw (rect); CGContext context = UIGraphics.GetCurrentContext(); context.SetAllowsAntialiasing(true); RectangleF currentBounds = this.Bounds; float dotsWidth = this.numberOfPages*kDotDiameter + Math.Max(0,this.numberOfPages-1)*kDotSpacer; float x = currentBounds.GetMidX() - dotsWidth/2; float y = currentBounds.GetMidY() - kDotDiameter/2; for (int i = 0; i < _numberOfPages; i++) { RectangleF circleRect = new RectangleF(x,y,kDotDiameter,kDotDiameter); if (i==_currentPage) { context.SetFillColor(this.CurrenColor); } else { context.SetFillColor(this.OtherColor); } context.FillEllipseInRect(circleRect); x += kDotDiameter + kDotSpacer; } } public override void TouchesBegan (MonoTouch.Foundation.NSSet touches, UIEvent evt) { base.TouchesBegan (touches, evt); PointF touchpoint = (touches.AnyObject as MonoTouch.UIKit.UITouch).LocationInView(this); RectangleF currentbounds = this.Bounds; float x = touchpoint.X- currentbounds.GetMidX(); if (x<0 && this.currentPage>=0) { this.currentPage--; Change(this,EventArgs.Empty); } else if (x>0 && this.currentPage<this.numberOfPages-1) { this.currentPage++; Change(this,EventArgs.Empty); } } #endregion #region delegate public event EventHandler Change; #endregion } 
0
source

All Articles