添加按鈕:
UIButton * btn = [UIButton buttonWithType:(UIButtonTypeRoundedRect)]; btn.frame = CGRectMake(100, 100, 100, 100); [btn addTarget:self action:@selector(btnPressed:) forControlEvents:(UIControlEventTouchUpInside)]; [btn setTitle:@"確定" forState:(UIControlStateNormal)]; [self.view addSubview:btn]; 添加label: UILabel * lbl = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 44)]; lbl.text = @"test"; lbl.textColor = [UIColor purpleColor]; lbl.font = [UIFont systemFontOfSize:18]; lbl.backgroundColor = [UIColor clearColor]; [self.view addSubview:lbl]; 添加view: UIView * aView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 80)]; aView.backgroundColor = [UIColor purpleColor]; [self.view addSubview:aView]; view為圓角: 需要:#import <QuartzCore/QuartzCore.h> aView.layer.cornerRadius = 10; 給view添加手勢: UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGesture)]; [self.view addGestureRecognizer:tap]; 添加textField以及delegate方法: 需要:UITextFieldDelegate協議 UITextField * text = [[UITextField alloc]initWithFrame:CGRectMake(0, 50, 320, 44)]; text.backgroundColor = [UIColor purpleColor]; text.placeholder = @"請輸入:"; text.borderStyle = UITextBorderStyleRoundedRect; text.font = [UIFont systemFontOfSize:18]; text.textAlignment = NSTextAlignmentLeft; //[text setContentMode:UIViewContentModeCenter]; [text setContentVerticalAlignment:(UIControlContentVerticalAlignmentCenter)];//內容在中間,上下 text.delegate = self; [self.view addSubview:text]; #pragma mark - textField delegate - (BOOL)textFieldShouldReturn:(UITextField *)textField{ [textField resignFirstResponder]; return YES; } textField的邊框寬度和邊框顏色: 需要:#import <QuartzCore/QuartzCore.h> text.layer.borderWidth = 1; text.layer.borderColor = [UIColor redColor].CGColor; 定義size大小: CGSize size = [str sizeWithFont:[UIFont systemFontOfSize:20] constrainedToSize:CGSizeMake(320, 1000) lineBreakMode:(NSLineBreakByWordWrapping)]; 播放音樂: 需要添加AVFoundation.framework框架 需要:#import <AVFoundation/AVFoundation.h> NSURL *url = [NSURL URLWithString:@"http://zhangmenshiting.baidu.com/data2/music/903505/8930817375600128.mp3?xcode=750b4fdc9f5eeb0954dfe54c6e23e3be24b7b177cb0d29e2&song_id=89308173"]; NSData *urlData = [NSData dataWithContentsOfURL:url]; NSError *error = nil; AVAudioPlayer * player = [[[AVAudioPlayer alloc]initWithData:urlData error:&error] autorelease]; //准備播放音樂 [player prepareToPlay]; [player play]; 添加風火輪: 需要添加 QuartzCore.framework框架 需要:#import <QuartzCore/QuartzCore.h> UIActivityIndicatorView * active = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyleWhiteLarge)]; active.backgroundColor = [UIColor grayColor]; active.alpha = 0.5; active.layer.cornerRadius = 10; active.frame = CGRectMake(100, 100, 80, 80); [self.view addSubview:active]; [active startAnimating];