Basic UI Elements
UITextField
1 2 3 4 5 6 7 8 | UITextField *textField =[[UITextField alloc] initWithFrame:CGRectMake(24.5, 65, 270, 30)]; textField.delegate=self; textField.textAlignment=UITextAlignmentCenter; textField.borderStyle=UITextBorderStyleRoundedRect; textField.placeholder=@"Usernamen"; textField.autocorrectionType=UITextAutocorrectionTypeNo; textField.autocapitalizationType=UITextAutocapitalizationTypeNone; [self.view addSubview: textField]; |
UIActionSheet
1 2 3 4 5 6 7 | UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"This is where the information will go" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"First", @"Second",@"Third",@"Cancel", nil]; actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque; actionSheet.destructiveButtonIndex = 3; // make the second button red (destructive) [actionSheet showInView:self.view]; [actionSheet release]; |
Selecting a button on UIActionSheet
1 2 3 4 5 6 | - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex == 0){ // do stuff } } |
UISwitch
1 2 3 | UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:CGRectZero]; mySwitch.on=NO; [self.view addSubview: mySwitch]; |
UILabel
1 2 3 4 | UILabel *usernameLabel = [[UILabel alloc] initWithFrame:CGRectZero]; usernameLabel.text=@"Personal Location Checkin"; usernameLabel.adjustsFontSizeToFitWidth=YES; [self.contentView addSubview:usernameLabel]; |
UISearchBar
1 2 3 4 5 6 7 8 | UISearchBar *mySearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.bounds.size.width, 45)]; mySearchBar.delegate = self; mySearchBar.showsCancelButton = YES; mySearchBar.barStyle=UIBarStyleBlackOpaque; mySearchBar.placeholder=@"Enter Name or Phone Number"; mySearchBar.keyboardType=UIKeyboardTypeNamePhonePad; [self.view addSubview: mySearchBar]; |
UIView
1 2 3 4 | UIView *myView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; myView.backgroundColor = [UIColor blackColor]; self.view = myView; [myView release]; |
UITableView
1 2 3 4 5 | UITableView *myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0, 45, self.view.bounds.size.width, 375)]; myTableView.delegate=self; myTableView.dataSource=self; [self.view addSubview: myTableView]; |
Or to make a grouped table view (like in preferences)
1 2 3 4 | UITableView *settingsTableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0, 40, self.view.bounds.size.width, 375) style:UITableViewStyleGrouped]; settingsTableView.delegate=self; settingsTableView.dataSource=self; [self.view addSubview:settingsTableView]; |
UINavigationBar
Plain no Title
1 2 | myNavBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.bounds.size.width, 45)]; [self.view addSubview:myNavBar]; |
Black Opaque (no title)
1 2 3 | myNavBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.bounds.size.width, 45)]; myNavBar.barStyle=UIBarStyleBlackOpaque; [self.view addSubview:myNavBar]; |
Black Translucent (no title)
1 2 3 | myNavBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.bounds.size.width, 45)]; myNavBar.barStyle=UIBarStyleBlackTranslucent; [self.view addSubview:myNavBar]; |
To add title and other buttons
1 2 | UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:@"Around Me"]; [myNavBar pushNavigationItem: navItem]; |
To add buttons with image
1 2 | myButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(action:)]; [navItem setLeftBarButtonItem: myButton]; |
Add button with Text
1 2 | myButton = [[UIBarButtonItem alloc] initWithTitle:@"About Us" style:UIBarButtonItemStyleBordered target:self action:@selector(action:)]; [navItem setRightBarButtonItem: myButton]; |
UIWebView
1 2 3 4 5 | myWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0.0, 45, self.view.bounds.size.width, 375)]; myWebView.delegate=self; myWebView.scalesPageToFit=YES; [myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]]]; [self.view addSubview: myWebView]; |
UITextView
1 2 3 4 5 6 7 8 | UITextView *textView = [[[UITextView alloc] initWithFrame:frame] autorelease]; textView.textColor = [UIColor blackColor]; textView.font = [UIFont fontWithName:@"Helvetica" size:15]; textView.delegate = self; textView.backgroundColor = [UIColor whiteColor]; textView.text = @"TEXT TO GO IN HERE"; [self.view addSubView:textView]; |
UISegmentedControl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems: [NSArray arrayWithObjects: [UIImage imageNamed:@"picture1.png"], [UIImage imageNamed:@"picture2.png"], [UIImage imageNamed:@"picture3.png"], [UIImage imageNamed:@"picture4.png"], nil]]; frame = CGRectMake( 0, 0, self.view.bounds.size.width, 35); segmentedControl.frame = frame; [segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged]; segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain; segmentedControl.selectedSegmentIndex = 1; [self.view addSubview:segmentedControl]; |
UIAlertView
1 2 3 4 | UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"UIAlertView" message:@"Your message goes here" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Button1", @"Button2", nil]; [alertView show]; [alertView release]; |
