未声明变量多位全局变量,未添加方法名多为viewDidLoad方法,所有方法均在控制器.m内。
GRectMake自定义视图"纯代码绘制视图":(添加到viewDidLoad方法中)
CGRect lblFrame = CGRectMake(110,10,200,40); //视图在屏幕上的位置与大小
UILabel *lblLocalized = [[UILabel alloc] initWithFrame:lblFrame]; //初始化一个UILabel
lblLocalized.backgroundColor = [UIColor clearColor]; //给视图背景色,这里设置透明
lblLocalized.text = NSLocalizedString(@"HelloKey",nil); //UILable的内容
[self.view addSubview:lblLocalized]; //添加到view上
设置view背景:(添加到viewDidLoad方法中)
[self.view setBackgroundColor:[UIColor redColor]];
提示对话框:(添加到viewDidLoad方法中)
UIActionSheet *mySheet = [[UIActionSheet alloc] initWithTitle:@"Email Deletion Options"
delegate:self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:@"Delete Everything"
otherButtonTitles:@"All Read Email", @"Spam Only",nil];
mySheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
[mySheet showInView:self.view];
等待对话框:(添加到viewDidLoad方法中)
[self.view setBackgroundColor:[UIColor blackColor]];//等待框是白色,所以设置背景突出视图
UIActivityIndicatorView *myActivityView = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
CGRect activityFrame = CGRectMake(130,100,50,50);
[myActivityView setFrame:activityFrame];
[myActivityView startAnimating];
[self.view addSubview:myActivityView];
提示对话框:(添加到viewDidLoad方法中)
UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"title"
message:@"This alert will disappear in 3 seconds"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[myAlert addButtonWithTitle:@"new button"];
[myAlert addButtonWithTitle:@"another button"];
[myAlert show];
UIButton和UICheckBox:(添加到viewDidLoad方法中)
//rounded-rect button
UIButton *roundedRectButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];//Button类型
CGRect buttonRect = CGRectMake(100,50,100,35);//位置大小
[roundedRectButton setFrame:buttonRect];
[roundedRectButton setTitle:@"Normal" forState:UIControlStateNormal];//按钮未按
[roundedRectButton setTitle:@"Highlighted" forState:UIControlStateHighlighted];//按钮按下
roundedRectButton.showsTouchWhenHighlighted = YES;//高亮
[roundedRectButton addTarget:self action:@selector(buttonDown:) forControlEvents:UIControlEventTouchDown];
[roundedRectButton addTarget:self action:@selector(buttonRelease:) forControlEvents:UIControlEventTouchUpInside];
//checkbox control
UIButton *checkbox = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect checkboxRect = CGRectMake(135,150,36,36);
[checkbox setFrame:checkboxRect];
[checkbox setImage:[UIImage imageNamed:@"checkbox_off.png"] forState:UIControlStateNormal];//普通状态
[checkbox setImage:[UIImage imageNamed:@"checkbox_on.png"] forState:UIControlStateSelected];//选中状态
[checkbox addTarget:self action:@selector(checkboxClick:) forControlEvents:UIControlEventTouchUpInside];
//custom circular button
UIButton *circularButton = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect circularRect = CGRectMake(80,220,165,164);
[circularButton setFrame:circularRect];
UIImage *buttonImage = [UIImage imageNamed:@"circular_button.png"];
[circularButton setImage:buttonImage forState:UIControlStateNormal];
//按钮按下
[circularButton addTarget:self action:@selector(buttonDown:) forControlEvents:UIControlEventTouchUpInside];
//add all the buttons to the main view
[self.view addSubview:roundedRectButton];
[self.view addSubview:checkbox];
[self.view addSubview:circularButton];
时间选择器
CGRect pickerFrame = CGRectMake(0,120,0,0);
UIDatePicker *myPicker = [[UIDatePicker alloc] initWithFrame:pickerFrame];
[myPicker addTarget:self action:@selector(pickerChanged:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:myPicker];
图片显示 并进行缩放
UIImage *anImage = [UIImage imageNamed:@"apple.png"];
UIImageView *myImageView = [[UIImageView alloc] initWithImage:anImage];
[[self view] addSubview:myImageView];
//scale our height and width by 50%
CGSize viewSize = myImageView.bounds.size;
viewSize.width = viewSize.width * 0.5; //make our width 50% smaller
viewSize.height = viewSize.height; //keep height the same
CGRect newFrame = CGRectMake(0,0,viewSize.width, viewSize.height);
[myImageView setFrame:newFrame];
myImageView.contentMode = UIViewContentModeScaleAspectFit;
图片切换
NSArray *arrImages = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:@"apple.png"],
[UIImage imageNamed:@"apple2.png"],
[UIImage imageNamed:@"apple3.png"],nil];
CGRect viewFrame = CGRectMake(0,0,200,200);
UIImageView *myImageView = [[UIImageView alloc] initWithFrame:viewFrame];
[myImageView setAnimationImages:arrImages];
[myImageView setAnimationRepeatCount:0];
[myImageView setAnimationDuration:0.5];
[self.view addSubview:myImageView];
[myImageView startAnimating];
显示多行字Label
CGRect labelFrame = CGRectMake(10,10,200,44);
UILabel *myLabel = [[UILabel alloc] initWithFrame:labelFrame];
myLabel.backgroundColor = [UIColor clearColor];
myLabel.textColor = [UIColor redColor];
myLabel.font = [UIFont fontWithName:@"Verdana" size:18.0];
myLabel.numberOfLines = 2;
myLabel.text = @"Hello World\nSecond line";
myLabel.shadowColor = [UIColor darkGrayColor];
myLabel.shadowOffset = CGSizeMake(1.0,1.0);
[self.view addSubview:myLabel];
分页视图UIPageControl
UIScrollView *myScrollView;
UIPageControl *myPageControl;
- (void)loadScrollViewWithPage:(UIView *)page
{
int pageCount = [[myScrollView subviews] count];
CGRect bounds = myScrollView.bounds;
bounds.origin.x = bounds.size.width * pageCount;
bounds.origin.y = 0;
page.frame = bounds;
[myScrollView addSubview:page];
}
- (void)createPages
{
CGRect pageRect = myScrollView.frame;
//create pages
UIView *page1 = [[UIView alloc] initWithFrame:pageRect];
page1.backgroundColor = [UIColor blueColor];
UIView *page2 = [[UIView alloc] initWithFrame:pageRect];
page2.backgroundColor = [UIColor redColor];
UIView *page3 = [[UIView alloc] initWithFrame:pageRect];
page3.backgroundColor = [UIColor greenColor];
//add to scrollview
[self loadScrollViewWithPage:page1];
[self loadScrollViewWithPage:page2];
[self loadScrollViewWithPage:page3];
}
- (void)viewDidLoad {
[super viewDidLoad];
float pageControlHeight = 18.0;
int pageCount = 3;
CGRect scrollViewRect = [self.view bounds];
scrollViewRect.size.height -= pageControlHeight;
//create scrollview
myScrollView = [[UIScrollView alloc] initWithFrame:scrollViewRect];
myScrollView.pagingEnabled = YES;
myScrollView.contentSize = CGSizeMake(scrollViewRect.size.width * pageCount,1);
myScrollView.showsHorizontalScrollIndicator = NO;
myScrollView.showsVerticalScrollIndicator = NO;
myScrollView.delegate = self;
//create pageview
CGRect pageViewRect = [self.view bounds];
pageViewRect.size.height = pageControlHeight;
pageViewRect.origin.y = scrollViewRect.size.height;
myPageControl = [[UIPageControl alloc] initWithFrame:pageViewRect];
myPageControl.backgroundColor = [UIColor blackColor];
myPageControl.numberOfPages = pageCount;
myPageControl.currentPage = 0;
[myPageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
//create pages
[self createPages];
//add to main view
[self.view addSubview:myScrollView];
[self.view addSubview:myPageControl];
}
- (void)scrollViewDidScroll:(UIScrollView *)sender
{
CGFloat pageWidth = sender.frame.size.width;
int page = floor((sender.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
myPageControl.currentPage = page;
}
- (void)changePage:(id)sender
{
int page = myPageControl.currentPage;
// update the scroll view to the appropriate page
CGRect frame = myScrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
[myScrollView scrollRectToVisible:frame animated:YES];
}
选择器UIPickerView
- (void)viewDidLoad {
[super viewDidLoad];
CGRect pickerFrame = CGRectMake(0,120,0,0);
UIPickerView *myPicker = [[UIPickerView alloc] initWithFrame:pickerFrame];
myPicker.dataSource = self;
myPicker.delegate = self;
myPicker.showsSelectionIndicator = YES;
[self.view addSubview:myPicker];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return 10;
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [NSString stringWithFormat:@"Row %i",row];
}
多项选择器UIPickerView
NSMutableArray *comp1;
NSMutableArray *comp2;
-(void)initComp1
{
comp1 = [[NSMutableArray alloc] init];
UIImageView *imgView;
imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"au.png"]];
[comp1 addObject:imgView];
imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ca.png"]];
[comp1 addObject:imgView];
imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"gb.png"]];
[comp1 addObject:imgView];
imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"nz.png"]];
[comp1 addObject:imgView];
imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"us.png"]];
[comp1 addObject:imgView];
}
-(void)initComp2
{
comp2 = [[NSMutableArray alloc] init];
UILabel *lbl;
lbl = [[UILabel alloc] initWithFrame:CGRectMake(0,0,100,44)];
lbl.backgroundColor = [UIColor clearColor];
lbl.text = @"Red";
[comp2 addObject:lbl];
lbl = [[UILabel alloc] initWithFrame:CGRectMake(0,0,100,44)];
lbl.backgroundColor = [UIColor clearColor];
lbl.text = @"Blue";
[comp2 addObject:lbl];
lbl = [[UILabel alloc] initWithFrame:CGRectMake(0,0,100,44)];
lbl.backgroundColor = [UIColor clearColor];
lbl.text = @"Yellow";
[comp2 addObject:lbl];
lbl = [[UILabel alloc] initWithFrame:CGRectMake(0,0,100,44)];
lbl.backgroundColor = [UIColor clearColor];
lbl.text = @"Pink";
[comp2 addObject:lbl];
lbl = [[UILabel alloc] initWithFrame:CGRectMake(0,0,100,44)];
lbl.backgroundColor = [UIColor clearColor];
lbl.text = @"Brown";
[comp2 addObject:lbl];
lbl = [[UILabel alloc] initWithFrame:CGRectMake(0,0,100,44)];
lbl.backgroundColor = [UIColor clearColor];
lbl.text = @"Black";
[comp2 addObject:lbl];
lbl = [[UILabel alloc] initWithFrame:CGRectMake(0,0,100,44)];
lbl.backgroundColor = [UIColor clearColor];
lbl.text = @"White";
[comp2 addObject:lbl];
}
- (void)viewDidLoad {
[super viewDidLoad];
//create some sample data
[self initComp1];
[self initComp2];
CGRect pickerFrame = CGRectMake(0,120,0,0);
UIPickerView *myPicker = [[UIPickerView alloc] initWithFrame:pickerFrame];
myPicker.dataSource = self;
myPicker.delegate = self;
myPicker.showsSelectionIndicator = YES;
[self.view addSubview:myPicker];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (component == 0)
return [comp1 count];
else
return [comp2 count];
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
if (component == 0)
return 100.0;
else
return 200.0;
}
- (UIView *)pickerView:(UIPickerView *)pickerView
viewForRow:(NSInteger)row
forComponent:(NSInteger)component
reusingView:(UIView *)view {
if (component == 0)
return [comp1 objectAtIndex:row];
else
return [comp2 objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSLog(@"row: %i, component:%i",row,component);
}
滚动条UIProgressView
NSTimer *timer;
- (void)updateProgress:(NSTimer *)sender
{
UIProgressView *progress = [sender userInfo];
//have we completed?
if (progress.progress == 1.0)
[timer invalidate];
else
progress.progress += 0.05;
}
- (void)viewDidLoad {
[super viewDidLoad];
UIProgressView *myProgressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
CGRect progressFrame = CGRectMake(10,100,300,25);
[myProgressView setFrame:progressFrame];
[myProgressView setProgress:0.0];
[self.view addSubview:myProgressView];
[myProgressView release];
//create timer
timer = [[NSTimer scheduledTimerWithTimeInterval:0.1
target:self
selector:@selector(updateProgress:)
userInfo:myProgressView
repeats:YES] retain];
}
scrollView
[super viewDidLoad];
CGRect scrollFrame = CGRectMake(20,90,280,280);
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:scrollFrame];
scrollView.minimumZoomScale = 0.5;
scrollView.maximumZoomScale = 2.0;
scrollView.delegate = self;
UIImage *bigImage = [UIImage imageNamed:@"appleLogo.jpg"];
largeImageView = [[UIImageView alloc] initWithImage:bigImage];
[scrollView addSubview:largeImageView];
scrollView.contentSize = largeImageView.frame.size; //important!
[self.view addSubview:scrollView];
标签视图UISegment
- (void)segmentClick:(id)sender
{
NSLog(@"you selected segment %d",[sender selectedSegmentIndex]);
}
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *arrSegments = [[NSArray alloc] initWithObjects:
[NSString stringWithString:@"0"],
[NSString stringWithString:@"1"],
[NSString stringWithString:@"2"],nil];
UISegmentedControl *mySegment = [[UISegmentedControl alloc] initWithItems:arrSegments];
CGRect segmentRect = CGRectMake(10,50,300,40);
[mySegment setFrame:segmentRect];
[mySegment addTarget:self action:@selector(segmentClick:) forControlEvents:UIControlEventValueChanged];
[mySegment setSegmentedControlStyle:UISegmentedControlStyleBar];
[mySegment setTintColor:[UIColor darkGrayColor]];
//select first item
[mySegment setSelectedSegmentIndex:0];
//change a segment size
[mySegment setWidth:120.0 forSegmentAtIndex:1];
//add a new segment
[mySegment insertSegmentWithTitle:@"new" atIndex:2 animated:YES];
//add segment to main view
[self.view addSubview:mySegment];
}
进度条UISlider
UILabel *lblSliderValue;
-(void)sliderAction:(id)sender
{
int stepAmount = 10;
float stepValue = (abs([(UISlider *)sender value]) / stepAmount) * stepAmount;
[sender setValue:stepValue];
lblSliderValue.text = [NSString stringWithFormat:@"%d",(int)stepValue];
}
- (void)viewDidLoad {
[super viewDidLoad];
CGRect sliderRect = CGRectMake(20,50,280,40);
UISlider *mySlider = [[UISlider alloc] initWithFrame:sliderRect];
mySlider.minimumValue = 0;
mySlider.maximumValue = 100;
mySlider.continuous = YES;
//images
UIImage *leftTrackImage = [[UIImage imageNamed:@"left_slider.png"] stretchableImageWithLeftCapWidth:5.0 topCapHeight:0.0];
UIImage *rightTrackImage = [[UIImage imageNamed:@"right_slider.png"] stretchableImageWithLeftCapWidth:5.0 topCapHeight:0.0];
UIImage *thumbImage = [UIImage imageNamed:@"apple_thumb.png"];
UIImage *minSliderImage = [UIImage imageNamed:@"apple_min.png"];
UIImage *maxSliderImage = [UIImage imageNamed:@"apple_max.png"];
[mySlider setThumbImage:thumbImage forState:UIControlStateNormal];
[mySlider setMinimumTrackImage:leftTrackImage forState:UIControlStateNormal];
[mySlider setMaximumTrackImage:rightTrackImage forState:UIControlStateNormal];
[mySlider setMinimumValueImage:minSliderImage];
[mySlider setMaximumValueImage:maxSliderImage];
[mySlider setValue:50.0f];
//handle value change events
[mySlider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
//label to show current value
CGRect lblRect = CGRectMake(145,100,100,20);
lblSliderValue = [[UILabel alloc] initWithFrame:lblRect];
lblSliderValue.backgroundColor = [UIColor clearColor];
lblSliderValue.text = [NSString stringWithFormat:@"%d",(int)mySlider.value];
//add slider to main view
[self.view addSubview:mySlider];
[self.view addSubview:lblSliderValue];
}
UITextField
- (void)viewDidLoad {
[super viewDidLoad];
CGRect textRect = CGRectMake(10,10,300,31);
UITextField *myTextField = [[UITextField alloc] initWithFrame:textRect];
myTextField.borderStyle = UITextBorderStyleRoundedRect;
myTextField.font = [UIFont systemFontOfSize:22.0];
myTextField.adjustsFontSizeToFitWidth = YES;
myTextField.minimumFontSize = 2.0;
myTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
myTextField.keyboardType = UIKeyboardTypeDefault;
myTextField.autocorrectionType = UITextAutocorrectionTypeNo;
myTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
myTextField.returnKeyType = UIReturnKeyDone;
//myTextField.secureTextEntry = YES;
myTextField.delegate = self;
[self.view addSubview:myTextField];
//force keyboard to show immediately
[myTextField becomeFirstResponder];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
//limit text field to 10 chars
int MAX_CHARS = 10;
NSMutableString *newText = [NSMutableString stringWithString:textField.text];
[newText replaceCharactersInRange:range withString:string];
return ([newText length] <= MAX_CHARS);
}
文本输入
UIScrollView *myScrollView;
UITextView *myTextView;
-(void)buttonClick:(id)sender
{
[myTextView resignFirstResponder];
}
- (void)viewDidLoad {
[super viewDidLoad];
CGRect viewRect = [self.view bounds];
myScrollView = [[UIScrollView alloc] initWithFrame:viewRect];
myScrollView.contentSize = viewRect.size;
[self.view addSubview:myScrollView];
CGRect buttonFrame = CGRectMake(10,10,60,32);
UIButton *keyboardToggle = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[keyboardToggle setTitle:@"hide" forState:UIControlStateNormal];
[keyboardToggle addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
keyboardToggle.frame = buttonFrame;
[myScrollView addSubview:keyboardToggle];
CGRect textRect = CGRectMake(10,60,300,200);
myTextView = [[UITextView alloc] initWithFrame:textRect];
myTextView.font = [UIFont systemFontOfSize:22.0];
myTextView.keyboardType = UIKeyboardTypeDefault;
myTextView.returnKeyType = UIReturnKeyGo;
//uncomment to enable data detectors
//myTextView.text = @"this is a link: http://google.com ";
//myTextView.dataDetectorTypes = UIDataDetectorTypeAll;
//myTextView.enabled = NO;
myTextView.delegate = self;
[myScrollView addSubview:myTextView];
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
float keyboardHeight;
if ([UIDevice currentDevice].orientation == UIDeviceOrientationPortrait | UIDeviceOrientationPortraitUpsideDown)
keyboardHeight = 216.0;
else
keyboardHeight = 162.0;
CGRect textViewRect = textView.frame;
float textViewBottom = textViewRect.origin.y + textViewRect.size.height;
CGRect viewRect = [myScrollView bounds];
float keyboardTop = viewRect.size.height-keyboardHeight;
float scrollOffset = fabs(textViewBottom - keyboardTop);
[myScrollView setContentOffset:CGPointMake(0, scrollOffset) animated:YES];
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
[myScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
}
UIToolbar
- (void)buttonClick:(id)sender {
NSLog(@"you clicked button: %@",[sender title]);
}
- (void)viewDidLoad {
[super viewDidLoad];
CGSize viewSize = self.view.frame.size;
float toolbarHeight = 44.0;
CGRect toolbarFrame = CGRectMake(0,viewSize.height-toolbarHeight,viewSize.width,toolbarHeight);
UIToolbar *myToolbar = [[UIToolbar alloc] initWithFrame:toolbarFrame];
myToolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth
|
UIViewAutoresizingFlexibleLeftMargin
|
UIViewAutoresizingFlexibleRightMargin
|
UIViewAutoresizingFlexibleTopMargin;
UIBarButtonItem *button1 = [[UIBarButtonItem alloc] initWithTitle:@"button 1"
style:UIBarButtonItemStylePlain target:self
action:@selector(buttonClick:)];
UIBarButtonItem *button2 = [[UIBarButtonItem alloc] initWithTitle:@"button 2"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(buttonClick:)];
UIBarButtonItem *button3 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"apple_icon.png"]
style:UIBarButtonItemStyleBordered
target:self
action:@selector(buttonClick:)];
UIBarButtonItem *flexButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
UIBarButtonItem *trashButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash
target:self
action:@selector(buttonClick:)];
NSArray *buttons = [[NSArray alloc] initWithObjects:button1,button2,button3, flexButton,trashButton,nil];
[myToolbar setItems:buttons animated:NO];
[self.view addSubview:myToolbar];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
自定义UIView
- (void)viewDidLoad {
[super viewDidLoad];
CGPoint frameCenter = self.view.center;
float width = 50.0;
float height = 50.0;
CGRect viewFrame = CGRectMake(frameCenter.x-width,frameCenter.y-height, width*2, height*2);
UIView *myView = [[UIView alloc] initWithFrame:viewFrame];
myView.backgroundColor = [UIColor blueColor];
//create subview
CGRect subViewFrame = CGRectInset(myView.bounds, width/2.0, height/2.0);
UIView *mySubview = [[UIView alloc] initWithFrame:subViewFrame];
mySubview.backgroundColor = [UIColor yellowColor];
//set autoresizing mask
mySubview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[myView addSubview:mySubview];
[[self view] addSubview:myView];
//animate resize
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
viewFrame = CGRectInset(viewFrame, -width, -height);
[myView setFrame:viewFrame];
[UIView commitAnimations];
}
UIWebView
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor blackColor]];
CGRect webRect = CGRectMake(10,10,300,380);
UIWebView *myWebView = [[UIWebView alloc] initWithFrame:webRect];
myWebView.scalesPageToFit = YES;
myWebView.delegate = self;
NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[myWebView loadRequest:request];
[self.view addSubview:myWebView];
activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[activity setCenter:CGPointMake(160,420)];
[self.view addSubview:activity];
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
[activity startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[activity stopAnimating];
[webView stringByEvaluatingJavaScriptFromString:@"alert('Finished Loading!');"];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
[activity stopAnimating];
NSLog(@"Error: %@",error);
}