Skip to main content

iOS Custom badge control

By Blog

Ever needed to create custom badge to UITabBarItem? Well here you go. I think it is pretty selfexplanatory.

public static class UITabBarItem_CustomBadge
	{
		public static void SetCustomBadgeValue (this UITabBarItem tabBarItem, string value, UIFont font, UIColor textColor, UIColor borderColor)
		{
			UIView view = (UIView)tabBarItem.ValueForKey (new NSString ("view"));

			tabBarItem.BadgeValue = value;

			// remove old one
			foreach (UIView subview in view.Subviews)
			{
				if (subview.Tag == 99) // badge has tag of 99
				{
					subview.RemoveFromSuperview ();

					break;
				}
			}

			foreach (UIView subview in view.Subviews)
			{
				if (subview.ToString ().StartsWith ("<_UIBadgeView:"))
				{
					// create new label view that we can style.
					// remove background, add border, make it nice
					var label = new UILabel (subview.Frame);

					label.TextAlignment = UITextAlignment.Center;
					label.Font = font;
					label.Text = value;
					label.BackgroundColor = borderColor.ColorWithAlpha (.5f);
					label.TextColor = textColor;
					label.Layer.BorderColor = borderColor.CGColor;
					label.Layer.BorderWidth = 1;
					label.Layer.CornerRadius = label.Frame.Height / 2;
					label.Layer.MasksToBounds = true;

					view.AddSubview (label);
					subview.Hidden = true;

					label.Tag = 99; // fake it here for iOS ;)
				}
			}
		}
	}

Enjoy and happy coding!

Paper for Facebook image scroll in Xamarin.iOS

By Blog

Paper for Facebook has some nice and neat features. One of them is scroll of an image using the tilt of the device.

Here is the implementation of such feature in Xamarin.iOS.

public override void ViewDidLoad ()
{
	base.ViewDidLoad ();
	
	// Perform any additional setup after loading the view, typically from a nib.
	mainScrollView.Bounces = false;
	mainScrollView.UserInteractionEnabled = false;

	var movingImageView = new UIImageView (UIImage.FromFile ("landscape.jpg"));
	mainScrollView.AddSubview (movingImageView);

	mainScrollView.ContentSize = new SizeF (movingImageView.Frame.Width, mainScrollView.Frame.Height);
	mainScrollView.ContentOffset = new PointF ((float)(mainScrollView.ContentSize.Width - View.Frame.Width) / 2.0f, 0);

	motionManager = new CMMotionManager ();
	motionManager.GyroUpdateInterval = 1 / 60.0;

	// this is how fast the image should move when rotate the device
	var motionMovingRate = 4;
	var shakeThreshold = 0.1;

	//get the max and min offset x value
	var maxXOffset = mainScrollView.ContentSize.Width - mainScrollView.Frame.Width;
	var minXOffset = 0;

	motionManager.StartGyroUpdates (NSOperationQueue.CurrentQueue, (CMGyroData gyroData, NSError error) => {
		if (Math.Abs (gyroData.RotationRate.y) >= shakeThreshold) 
		{
			var targetX = mainScrollView.ContentOffset.X - gyroData.RotationRate.y * motionMovingRate;

			if (targetX > maxXOffset)
						targetX = maxXOffset;
			else if (targetX < minXOffset)
						targetX = minXOffset;

			mainScrollView.ContentOffset = new PointF ((float)targetX, 0);
		}
	});
}

Download the source code and project from here.

Happy coding!

IOS6 style Navigation Bar in IOS7

By Blog

So you need to create that old style look in an app, maybe because it is a legacy code and your client needs this because of approved style guides and new ones do not exist for IOS7 yet?

Then do not despair, it is easier than you think. I used appearance for UINavigationBar to set up the basics.

[[UINavigationBar appearance] setTitleTextAttributes: @{ UITextAttributeFont : SOME_FONT_IF_YOU_WISH }];
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"NavBar.png"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBarStyle:UIBarStyleBlack];

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
        self.window.clipsToBounds = YES;
    }

Then you need to make sure all the views are not offset by 64pixels down. It is again very simple, just force the inset to be used for non transparent navigation bar too. I call this in viewDidLoad and viewWillAppear.

- (void)updateInsetIOS7
{
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        self.edgesForExtendedLayout = UIRectEdgeAll;
        self.extendedLayoutIncludesOpaqueBars = YES;
    }
}

Happy coding!

Official London Guide featured by Apple as Editor’s choice!

By Blog

Absolutely amazing news and cannot express how exited I’m, application we worked very hard for our client has been picked as Editor’s choice just at the moment when Olympics ceremony to be started. Download while it is hot!

http://itunes.apple.com/gb/app/london-official-city-guide/id536603270?mt=8