RSS

Category Archives: MonoTouch

MonoTouch Tips

OS4 Installed

Just downloaded and installed iPhone SDK 4.0.1 and looking forward to exploring the new features.

 
Leave a comment

Posted by on July 27, 2010 in MonoTouch

 

MonoTouch Tip #3 – Moving the View

Following on from Tip #2, this tip describes how to move the view to ensure that when a user touches down on a text box it will remain visible. By default the Keyboard uses up about half the screen area. If your text box was positioned within this area it would automatically become covered by the Keyboard. To avoid this first set up the TextFieldShouldReturn event in the ViewDidLoad OverRide. This code assumes you have a textbox called txtBox on your view placed in the bottom half of your view to show the scrolling effect.

//Add these variables to your view class...
float scrollamount = 0.0f;
float bottomPoint = 0.0f;
bool moveViewUp = false;

//Modify the ViewDidLoad OverRide...
public override void ViewDidLoad ()
{
base.ViewDidLoad ();

this.txtBox.ShouldReturn += TextFieldShouldReturn;
}

private bool TextFieldShouldReturn (UITextField tf)
{
tf.ResignFirstResponder ();
if (moveViewUp) { ScrollTheView(false); }
return true;
}

I have kept the TextFieldShouldReturn function generic so it can handle any text box in the view. To calculate and scroll up the view we need to add code to the Keyboard Notification handler we created in Tip #2.


private void KeyboardUpNotification (NSNotification notification)
{
ResetTheView();

RectangleF r = UIKeyboard.BoundsFromNotification(notification);

if(this.txtBox.IsEditing)
{
//Calculate the bottom of the Texbox
//plus a small margin...

bottomPoint = (this.txtBox.Frame.Y + this.txtBox.Frame.Height + yOffset);

//Calculate the amount to scroll the view
//upwards so the Textbox becomes visible...
//This is the height of the Keyboard -
//(the height of the display - the bottom
//of the Texbox)...

scrollamount = (r.Height - (View.Frame.Size.Height - bottomPoint));
}

//Check to see whether the view
//should be moved up...

if (scrollamount > 0)
{
moveViewUp = true;
ScrollTheView(moveViewUp);
}
else moveViewUp = false;
}

private void ResetTheView()
{
UIView.BeginAnimations(string.Empty, System.IntPtr.Zero);
UIView.SetAnimationDuration(0.3);

RectangleF frame = View.Frame;
frame.Y = 0;
View.Frame = frame;
UIView.CommitAnimations();
}

So what’s going on here. In the KeyboardUpNotification handler we call the ResetTheView method to ensure the view is always reset to the default position on the screen. We then get the Keyboard bounds to calculate the scroll amount for the text box which is in editing mode. Next we need to create the ScrollTheView method to increase or decrease the Y axis of the view and animate the scroll.

private void ScrollTheView(bool movedUp)
{
//To invoke a views built-in animation behaviour,
//you create an animation block and
//set the duration of the move...
//Set the display scroll animation and duration...

UIView.BeginAnimations(string.Empty, System.IntPtr.Zero);
UIView.SetAnimationDuration(0.3);

//Get Display size...
RectangleF frame = View.Frame;

if (movedUp) {
//If the view should be moved up,
//subtract the keyboard height from the display...

frame.Y -= scrollamount;
}
else {
//If the view shouldn't be moved up, restore it
//by adding the keyboard height back to the original...

frame.Y += scrollamount;
}

//Assign the new frame to the view...
View.Frame = frame;

//Tell the view that your all done with setting
//the animation parameters, and it should
//start the animation...

UIView.CommitAnimations();

}

This code assumes you have a textbox called txtBox on your view placed in the bottom half of your view to show the scrolling effect.

 
2 Comments

Posted by on May 12, 2010 in MonoTouch

 

MonoTouch Tip #2 – Adding Keyboard Support.

To add support for keyboard input to your MonoTouch application you must first register for Notifications after your view as loaded.

NSObject obs1;

public override void ViewDidLoad ()
{
obs1 = NSNotificationCenter.DefaultCenter.AddObserver (
"UIKeyboardDidShowNotification", KeyboardUpNotification);
}

The next task is to set up the Notification method.

private void KeyboardUpNotification (NSNotification notification)
{
//Get the bounds of the keyboard...
RectangleF r = UIKeyboard.BoundsFromNotification(notification);
}

The keyboard bounds can then be used to move the view to keep textbox inputs visible. You will also need to wire up the textbox ShouldReturn event for each of your input text-boxes to scroll the view down when you are finished with the keyboard.

 
Leave a comment

Posted by on May 11, 2010 in MonoTouch

 

MonoTouch Tip #1 – Creating a simple Message Box function

Even in the most simple applications you will often need to display messages to the user. Hopefully it will be just useful information and only occasionally an error message. If you have created a single view iPhone application it may be an overkill to create an error handling framework. This sample will add simple Message Box functionality to your view class.

[Flags]
internal enum MButtons
{
OK = 1,
Cancel = 2
};

private void MessageBox(string title, string message, MButtons buttons)
{
using(UIAlertView alert = new UIAlertView())
{
alert.Title = title;
alert.Message = message;

if((buttons & MButtons.OK) == MButtons.OK)
{
alert.AddButton("OK");
}

if((buttons & MButtons.Cancel) == MButtons.Cancel)
{
alert.AddButton("Cancel");
}

alert.Show();
}
}

Here is an example using this function:

MessageBox("My Message Box",
"This could be information or error details",
MButtons.OK | MButtons.Cancel);

I have found this snippet of code extremely useful when creating a quick application or prototyping a larger interface.

 
Leave a comment

Posted by on May 11, 2010 in MonoTouch

 

My first iPhone application.

Pressure Logo
This is my first venture into iPhone development, so it’s a very simple app for recording your vehicle tyre pressures. I also included a handy Bar to PSI converter. I used C Sharp as the programming language and Mono Touch. The most difficult functionality to get working correctly was moving the view to make way for the keyboard and showing the textboxes just above it.  I’m working on the next version now which will give multi vehicle functionality. I will let you know how that is going in the next few days. Click on the image to view version 1.0 on the App store:

 
Leave a comment

Posted by on May 8, 2010 in MonoTouch

 

Welcome to my new blog!

Welcome to my new blog. I intend to update this regularly with my tales of learning the art of Smart Phone App development. Smart Phone App development is a hobby and my main job is as a Silverlight Developer on a major document management solution. See my about page for more information on my career. The Smart Phone market has really taken off in the last few years and I thought it was about time I got involved. The iPhone and iPad platform is challenging for a programmer used to larger formats and I will be looking at learning Objective C in the next few months. Maybe you will want to follow along with my journey and even offer some helpful comments along the way.