注:代码摘自苹果官方"UICatalog"源码:
1. UIButton
UIButton *detailDisclosureButtonType = [[UIButton buttonWithType:UIButtonTypeDetailDisclosure] retain];
detailDisclosureButtonType.frame = CGRectMake(250.0, 8.0, 25.0, 25.0);
[detailDisclosureButtonType setTitle:@"Detail Disclosure" forState:UIControlStateNormal];
detailDisclosureButtonType.backgroundColor = [UIColor clearColor];
[detailDisclosureButtonType addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
+ (UIButton *)buttonWithTitle: (NSString *)titletarget:(id)targetselector:(SEL)selectorframe:(CGRect)frame
image:(UIImage *)imageimagePressed:(UIImage *)imagePresseddarkTextColor:(BOOL)darkTextColor
{
UIButton *button = [[UIButton alloc] initWithFrame:frame];
// or you can do this:
// UIButton *button = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
// button.frame = frame;
button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[button setTitle:title forState:UIControlStateNormal];
if (darkTextColor)
{
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
}
else
{
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}
UIImage *newImage = [image stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];
[button setBackgroundImage:newImage forState:UIControlStateNormal];
UIImage *newPressedImage = [imagePressed stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];
[button setBackgroundImage:newPressedImage forState:UIControlStateHighlighted];
[button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
// in case the parent view draws with a custom color or gradient, use a transparent color
button.backgroundColor = [UIColor clearColor];
return button;
}
2.UISwitch
- (UISwitch *)switchCtl
{
if (switchCtl == nil)
{
CGRect frame = CGRectMake(198.0, 12.0, 94.0, 27.0);
switchCtl = [[UISwitch alloc] initWithFrame:frame];
[switchCtl addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
// in case the parent view draws with a custom color or gradient, use a transparent color
switchCtl.backgroundColor = [UIColor clearColor];
[switchCtl setAccessibilityLabel:NSLocalizedString(@"StandardSwitch", @"")];
switchCtl.tag = kViewTag; // tag this view for later so we can remove it from recycled table cells
}
return switchCtl;
}
3.UISlider
- (UISlider *)sliderCtl
{
if (sliderCtl == nil)
{
CGRect frame = CGRectMake(174.0, 12.0, 120.0, kSliderHeight);
sliderCtl = [[UISlider alloc] initWithFrame:frame];
[sliderCtl addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
// in case the parent view draws with a custom color or gradient, use a transparent color
sliderCtl.backgroundColor = [UIColor clearColor];
sliderCtl.minimumValue = 0.0;
sliderCtl.maximumValue = 100.0;
sliderCtl.continuous = YES;
sliderCtl.value = 50.0;
// Add an accessibility label that describes the slider.
[sliderCtl setAccessibilityLabel:NSLocalizedString(@"StandardSlider", @"")];
sliderCtl.tag = kViewTag; // tag this view for later so we can remove it from recycled table cells
}
return sliderCtl;
}
4.UIPageControl
- (UIPageControl *)pageControl
{
if (pageControl == nil)
{
CGRect frame = CGRectMake(120.0, 14.0, 178.0, 20.0);
pageControl = [[UIPageControl alloc] initWithFrame:frame];
[pageControl addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventTouchUpInside];
// in case the parent view draws with a custom color or gradient, use a transparent color
pageControl.backgroundColor = [UIColor grayColor];
pageControl.numberOfPages = 10; // must be set or control won't draw
pageControl.tag = kViewTag; // tag this view for later so we can remove it from recycled table cells
}
return pageControl;
}
5.UIActivityIndicatorView
- (UIActivityIndicatorView *)progressInd
{
if (progressInd == nil)
{
CGRect frame = CGRectMake(265.0, 12.0, kProgressIndicatorSize, kProgressIndicatorSize);
progressInd = [[UIActivityIndicatorView alloc] initWithFrame:frame];
[progressInd startAnimating];
progressInd.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
[progressInd sizeToFit];
progressInd.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleBottomMargin);
progressInd.tag = kViewTag; // tag this view for later so we can remove it from recycled table cells
}
return progressInd;
}
6.UIProgressView
- (UIProgressView *)progressBar
{
if (progressBar == nil)
{
CGRect frame = CGRectMake(126.0, 20.0, kUIProgressBarWidth, kUIProgressBarHeight);
progressBar = [[UIProgressView alloc] initWithFrame:frame];
progressBar.progressViewStyle = UIProgressViewStyleDefault;
progressBar.progress = 0.5;
progressBar.tag = kViewTag; // tag this view for later so we can remove it from recycled table cells
}
return progressBar;
}
7.UITextField
- (UITextField *)textFieldNormal
{
if (textFieldNormal == nil)
{
CGRect frame = CGRectMake(kLeftMargin, 8.0, kTextFieldWidth, kTextFieldHeight);
textFieldNormal = [[UITextField alloc] initWithFrame:frame];
textFieldNormal.borderStyle = UITextBorderStyleBezel;
textFieldNormal.textColor = [UIColor blackColor];
textFieldNormal.font = [UIFont systemFontOfSize:17.0];
textFieldNormal.placeholder = @"<enter text>";
textFieldNormal.backgroundColor = [UIColor whiteColor];
textFieldNormal.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
textFieldNormal.keyboardType = UIKeyboardTypeDefault; // use the default type input method (entire keyboard)
textFieldNormal.returnKeyType = UIReturnKeyDone;
textFieldNormal.clearButtonMode = UITextFieldViewModeWhileEditing; // has a clear 'x' button to the right
textFieldNormal.tag = kViewTag; // tag this control so we can remove it later for recycled cells
textFieldNormal.delegate = self; // let us be the delegate so we know when the keyboard's "Done" button is pressed
// Add an accessibility label that describes what the text field is for.
[textFieldNormal setAccessibilityLabel:NSLocalizedString(@"NormalTextField", @"")];
}
return textFieldNormal;
}
8.UITextView(UITextViewDelegate)
- (void)setupTextView
{
self.textView = [[[UITextView alloc] initWithFrame:self.view.frame] autorelease];
self.textView.textColor = [UIColor blackColor];
self.textView.font = [UIFont fontWithName:@"Arial" size:18];
self.textView.delegate = self;
self.textView.backgroundColor = [UIColor whiteColor];
self.textView.text = @"Now is the time for all good developers to come to serve their country.\n\nNow is the time for all good developers to come to serve their country.";
self.textView.returnKeyType = UIReturnKeyDefault;
self.textView.keyboardType = UIKeyboardTypeDefault; // use the default type input method (entire keyboard)
self.textView.scrollEnabled = YES;
// this will cause automatic vertical resize when the table is resized
self.textView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
// note: for UITextView, if you don't like autocompletion while typing use:
// myTextView.autocorrectionType = UITextAutocorrectionTypeNo;
[self.view addSubview: self.textView];
}
TextView键盘处理:
- (void)viewWillAppear:(BOOL)animated
{
// listen for keyboard hide/show notifications so we can properly adjust the table's height
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWillShow:(NSNotification *)aNotification
{
// the keyboard is showing so resize the table's height
CGRect keyboardRect = [[[aNotification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue];
NSTimeInterval animationDuration = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
CGRect frame = self.view.frame;
frame.size.height -= keyboardRect.size.height;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame = frame;
[UIView commitAnimations];
}
- (void)keyboardWillHide:(NSNotification *)aNotification
{
// the keyboard is hiding reset the table's height
CGRect keyboardRect = [[[aNotification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue];
NSTimeInterval animationDuration = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
CGRect frame = self.view.frame;
frame.size.height += keyboardRect.size.height;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame = frame;
[UIView commitAnimations];
}
9. UISegmentedControl
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:
[UIImage imageNamed:@"segment_check.png"],[UIImage imageNamed:@"segment_search.png"],[UIImage imageNamed:@"segment_tools.png"],nil]];
yPlacement += kTweenMargin + kLabelHeight;
frame = CGRectMake( kLeftMargin,yPlacement,self.view.bounds.size.width - (kRightMargin * 2.0),kSegmentedControlHeight);
segmentedControl.frame = frame;
[segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
segmentedControl.selectedSegmentIndex = 1;
NSArray *segmentTextContent = [NSArray arrayWithObjects: @"Check", @"Search", @"Tools", nil];
segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentTextContent];
yPlacement += kTweenMargin + kLabelHeight;
frame = CGRectMake( kLeftMargin,yPlacement,self.view.bounds.size.width - (kRightMargin * 2.0),kSegmentedControlHeight);
segmentedControl.frame = frame;
[segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBordered;
segmentedControl.selectedSegmentIndex = 1;
10.UIToolBar
// match each of the toolbar item's style match the selection in the "UIBarButtonItemStyle" segmented control
UIBarButtonItemStyle style =UIBarButtonItemStylePlain; //UIBarButtonItemStyleBordered、UIBarButtonItemStyleDone
// create the system-defined "OK or Done" button
UIBarButtonItem *systemItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:currentSystemItemtarget:self action:@selector(action:)];
systemItem.style = style;
// flex item used to separate the left groups items and right grouped items
UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpacetarget:nilaction:nil];
// create a special tab bar item with a custom image and title
UIBarButtonItem *infoItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"segment_tools.png"] style:styletarget:self
action:@selector(action:)];
// Set the accessibility label for an image bar item.
[infoItem setAccessibilityLabel:NSLocalizedString(@"ToolsIcon", @"")];
// create a bordered style button with custom title
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithTitle:@"Item"style:style // note you can use "UIBarButtonItemStyleDone" to make it blue
target:selfaction:@selector(action:)];
NSArray *items = [NSArray arrayWithObjects: systemItem, flexItem, customItem, infoItem, nil];
[self.toolbar setItems:items animated:NO];
11. UIAlertView、UIActionSheet
// open a dialog with an OK and cancel button
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"UIActionSheet <title>"
delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"OK" otherButtonTitles:nil];
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
[actionSheet showInView:self.view]; // show from our table view (pops up in the middle of the table)
[actionSheet release];
// open a dialog with two custom buttons
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"UIActionSheet <title>"
delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil
otherButtonTitles:@"Button1", @"Button2", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
actionSheet.destructiveButtonIndex = 1; // make the second button red (destructive)
[actionSheet showInView:self.view]; // show from our table view (pops up in the middle of the table)
[actionSheet release];
// open a alert with an OK and cancel button
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"UIAlertView" message:@"<Alert message>"
delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
[alert show];
[alert release];
// open an alert with two custom buttons
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"UIAlertView" message:@"<Alert message>"
delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Button1", @"Button2", nil];
[alert show];
[alert release];
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
// the user clicked one of the OK/Cancel buttons
}
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
}