一、禁止横屏旋转 1、#pragma mark - 禁止横屏 -(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { returnUIInterfaceOrientationMaskPortrait; } 2、//自动旋转 - (BOOL)shouldAutorotate { returnNO; } //支持的屏幕转向 - (UIInterfaceOrientationMask)supportedInterfaceOrientations { //使用 UIInterfaceOrientationPortrait横屏播放视频结束后,app依然横屏,而table回到竖屏,不与app保持一致 //return UIInterfaceOrientationPortrait; //使用 UIInterfaceOrientationMaskPortrait横屏播放视频结束后,app与table一起回到竖屏,保持了一致 returnUIInterfaceOrientationMaskPortrait; } 二、TabBarController 1、这个控制器是tableviewcontroller,当后续页面直接用pop返回root时,需要删除自带的tabbar -(void)viewWillLayoutSubviews{ [superviewWillLayoutSubviews]; for (UIView *childinself.tabBarController.tabBar.subviews) { if ([childisKindOfClass:NSClassFromString(@"UITabBarButton")]) { [child removeFromSuperview]; } } } for (UIView *childinself.tabBarController.tabBar.subviews) { if ([childisKindOfClass:[UIControlclass]]) { [child removeFromSuperview]; } } 2、隐藏底部tabbar //1.设置self.tabBarController.tabBar.hidden=YES; self.tabBarController.tabBar.hidden=YES; //2.如果在push跳转时需要隐藏tabBar,设置self.hidesBottomBarWhenPushed=YES; 三、NavigationController 1、设置导航条颜色 nav.navigationBar.barTintColor=[UIColorredColor]; [self.navigationController.navigationBarsetBarTintColor:COLOR_S_WHITE]; 2、设置给定控制状态标题的文本属性 [self.navigationController.navigationBarsetTitleTextAttributes:[NSDictionarydictionaryWithObjectsAndKeys:COLOR_S_BLACK,NSForegroundColorAttributeName,nil]]; 3、隐藏导航条 self.navigationController.navigationBarHidden =NO; 4、navigationBar的毛玻璃效果 。translucent设置为YES时,导航栏呈现半透明效果 iOS7之后由于navigationBar.translucent默认是YES,坐标零点默认在(0,0)点 当不透明的时候,零点坐标在(0,64);如果你想设置成透明的,而且还要零点从(0,64)开始,那就添加:self.edgesForExtendedLayout = UIRectEdgeNone; self.navigationController.navigationBar.translucent = YES; 5、// 隐藏导航栏下方的分割线 [self.navigationController.navigationBarsetBackgroundImage:[[UIImagealloc]init] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault]; [self.navigationController.navigationBarsetShadowImage:[UIImagenew]]; 6、设置导航条的字体颜色 nav.navigationBar.titleTextAttributes=[NSDictionarydictionaryWithObject:[UIColorredColor]forKey:NSForegroundColorAttributeName]; //导航标题字颜色 [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : COLOR_S_WHITE}]; 四、TableView 1、table的滚动 self.tableView.scrollEnabled =NO; //设置tableview 不能滚动 2、table一键回到顶部 (1)[self.tableView setContentOffset:CGPointMake(0, 0) animated:YES]; (本人使用这个奏效) (2)- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated; (3)设置scrollsToTop = YES;这个属性,点击状态栏就可以返回顶部了。 3、设置cell不可编辑 table.allowsSelection =NO; 4、scrollView滑动时键盘消失属性;(ios7版本才有) _table.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag; 五、UIScrollView 1、设置导航栏下的空白位置是否自动下移 //根据状态栏、导航栏、底部栏自动调节导航栏以下空白是否下移 self.automaticallyAdjustsScrollViewInsets =NO; 2、scrollView滑动时键盘消失属性;(ios7版本才有) _table.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag; 六、UIButton 1、设置控件边框颜色以及宽度 btn.layer.borderColor = [UIColorredColor].CGColor; btn.layer.borderWidth =1; 2.设置UIButton的文字显示位置、字体的大小、字体的颜色 //设置按钮上的自体的大小 //[btn setFont: [UIFont systemFontSize: 14.0]]; //这种可以用来设置字体的大小,但是可能会在将来的SDK版本中去除改方法 //应该使用 btn.titleLabel.font = [UIFont systemFontOfSize: 14.0]; tvnamelabel=[[UIButton alloc]initWithFrame:CGRectMake(5,5,200,40)]; 这样初始化的button,文字默认颜色是白色的,所有如果背景也是白色的话,是看不到文字的, btn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft ;//设置文字位置,现设为居左,默认的是居中 有些时候我们想让UIButton的title居左对齐,我们设置 btn.textLabel.textAlignment = UITextAlignmentLeft 是没有作用的,我们需要设置 btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight; 但是问题又出来,此时文字会紧贴到做边框,我们可以设置 btn.contentEdgeInsets = UIEdgeInsetsMake(0,10, 0, 0); 使文字距离做边框保持10个像素的距离。 设置UIButton上字体的颜色设置UIButton上字体的颜色,不是用: [btn.titleLabel setTextColor:[UIColorblackColor]]; btn.titleLabel.textColor=[UIColor redColor]; 而是用: [btn setTitleColor:[UIColor blackColor]forState:UIControlStateNormal]; 七、UILabel 给所有view设置圆角 给圆角view加阴影,传统加阴影的方法是不行的, 传统的方法就是: avatarImageView.layer.shadowColor = [UIColor blackColor].CGColor; avatarImageView.layer.shadowOffset = CGSizeMake(0, 1); avatarImageView.layer.shadowOpacity = 1; 因为setMasksToBounds表示对frame外的内容进行了裁减,只可显示frame内的内容。由于这种方法加的阴影在frame外,所以被裁减了。 传统方法不行,那我们可以把圆角的avatarImageView放到一个大小与它一样的的UIView中,让这个view有阴影,那效果看起来就一样了。 CGRect rect = CGRectMake(0, 0, 48, 48); avatarImageView = [[UIImageView alloc] initWithFrame:rect]; avatarImageView.image = [UIImage imageNamed:@"test.png"]; //Round the corners CALayer * layer = [avatarImageView layer]; [layer setMasksToBounds:YES]; [layer setCornerRadius:9.0]; //Add a shadow by wrapping the avatar into a container UIView * shadow = [[UIView alloc] initWithFrame: rect]; avatarImageView.frame = CGRectMake(0,0,rect.size.width, rect.size.height); // setup shadow layer and corner shadow.layer.shadowColor = [UIColor grayColor].CGColor; shadow.layer.shadowOffset = CGSizeMake(0, 1); shadow.layer.shadowOpacity = 1; shadow.layer.shadowRadius = 9.0; shadow.layer.cornerRadius = 9.0; shadow.clipsToBounds = NO; // combine the views [shadow addSubview: avatarImageView]; [self.view addSubView:shadow]; //设置圆角 view.layer.cornerRadius = height /2; view.layer.masksToBounds =YES; //view.clipsToBounds = YES; UIView 的 clipsToBounds属性和CALayer的setMasksToBounds属性表达的意思是一致的。 取值:BOOL(YES/NO) 作用:决定了子视图的显示范围。具体的说,就是当取值为YES时,剪裁超出父视图范围的子视图部分;当取值为NO时,不剪裁子视图。默认值为NO。 1、根据label里的文字来自动适应尺寸 [labelsizeToFit]; 2、根据label固定宽度来调节label的字体大小 adjustsFontSizeToFitWidth:文字内容自适应标签度,默认NO label.adjustsFontSizeToFitWidth = YES; 八、UITextField 1、设置文本框的样式 _textField.borderStyle =UITextBorderStyleRoundedRect; 2、属性returnKeyType设置return键类型 _textField.returnKeyType =UIReturnKeySend; 3、自动可用return键 _textField.enablesReturnKeyAutomatically =YES; 4、英文首字母是否大写 _textField.autocapitalizationType =UITextAutocapitalizationTypeNone; 5、清除键的显示方式 _textField.clearButtonMode =UITextFieldViewModeWhileEditing; 九、删除子视图 1、//依次遍历self.view中的所有子视图 for(id tmpView in [self.viewsubviews]) { //找到要删除的子视图的对象 if([tmpView isKindOfClass:[UIImageViewclass]]) { UIImageView *imgView = (UIImageView *)tmpView; if(imgView.tag == 1) //判断是否满足自己要删除的子视图的条件 { [imgView removeFromSuperview]; //删除子视图 break; //跳出for循环,因为子视图已经找到,无须往下遍历 } } } 2、 //依次遍历self.view中的所有子视图 for(UIButton *buttonin [cellsubviews]) { //找到要删除的子视图的对象 if([buttonisKindOfClass:[UIButtonclass]]) { [button removeFromSuperview]; } } 3、 [view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; 十、遮罩背景 //覆盖导航栏,将View添加在keyWindow上 UIWindow *win = [[UIApplicationsharedApplication]keyWindow]; topView = [win.subviewsfirstObject]; 十一、状态栏 [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault]; 如果状态栏背景为浅色,应选用黑色字样式(UIStatusBarStyleDefault,默认值);如果背景为深色,则选用白色字样式(UIStatusBarStyleLightContent)。 1、控件的局部圆角问题 一个button或者label,只要右边的两个角圆角,或者只要一个圆角。该怎么办呢?这就需要图层蒙版来帮助我们了 CGRect rect = CGRectMake(0, 0, 100, 50); CGSize radio = CGSizeMake(5, 5);//圆角尺寸 UIRectCorner corner = UIRectCornerTopLeft|UIRectCornerTopRight;//这只圆角位置 UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corner cornerRadii:radio]; CAShapeLayer *masklayer = [[CAShapeLayer alloc]init];//创建shapelayer masklayer.frame = button.bounds; masklayer.path = path.CGPath;//设置路径 button.layer.mask = masklayer; 举例为button,其它继承自UIView的控件都可以 2、navigationBar的透明问题 如果仅仅把navigationBar的alpha设为0的话,那就相当于把navigationBar给隐藏了,大家都知道,父视图的alpha设置为0的话,那么子视图全都会透明的。那么相应的navigationBar的标题和左右两个按钮都会消失。这样显然达不到我们要求的效果。 (1)如果仅仅是想要navigationBar透明,按钮和标题都在可以使用以下方法: [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];//给navigationBar设置一个空的背景图片即可实现透明,而且标题按钮都在 细心的你会发现上面有一条线如下图: 这就需要我们做进一步处理,把线去掉,如下方法即可: self.navigationController.navigationBar.shadowImage = [UIImage new]; //其实这个线也是image控制的。设为空即可 (2)如果你想在透明的基础上实现根据下拉距离,由透明变得不透明的效果,那么上面那个就显得力不从心了,这就需要我们采用另外一种方法了 //navigationBar是一个复合视图,它是有许多个控件组成的,那么我们就可以从他的内部入手 [[self.navigationController.navigationBar subviews] objectAtIndex:0].alpha = 0;//这里可以根据scrollView的偏移量来设置alpha就实现了渐变透明的效果 3、全局设置navigationBar标题的样式和barItem的标题样式 //UIColorWithHexRGB( )这个方法是自己定义的,这里只需要给个颜色就好了 [[UINavigationBar appearance] setBarTintColor:UIColorWithHexRGB(0xfefefe)]; [[UINavigationBar appearance] setTitleTextAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:18],NSForegroundColorAttributeName:UIColorWithHexRGB(0xfe6d27)}]; [[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:10],NSForegroundColorAttributeName : UIColorWithHexRGB(0x666666)} forState:UIControlStateNormal]; [[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSiz 4、navigationBar隐藏显示的过度 一个页面隐藏navigationBar,另一个不隐藏。两个页面进行push和pop的时候,尤其是有侧滑手势返回的时候,不做处理就会造成滑动返回时,navigationBar位置是空的,直接显示一个黑色或者显示下面一层视图,很难看。这就需要我们加入过度动画来隐藏或显示navigationBar: 在返回后将要出现的页面实现viewWillAppear方法,需要隐藏就设为YES,需要显示就设为NO - (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [self.navigationController setNavigationBarHidden:NO animated:YES]; } 5、给webView添加头视图 webView是一个复合视图,里面包含有一个scrollView,scrollView里面是一个UIWebBrowserView(负责显示WebView的内容) UIView *webBrowserView = self.webView.scrollView.subviews[0];//拿到webView的webBrowserView self.backHeadImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenWidth*2/3.0)]; [_backHeadImageView sd_setImageWithURL:[NSURL URLWithString:self.imageUrl] placeholderImage:[UIImage imageNamed:@"placeholderImage"]]; [self.webView insertSubview:_backHeadImageView belowSubview:self.webView.scrollView]; //把backHeadImageView插入到webView的scrollView下面 CGRect frame = self.webBrowserView.frame; frame.origin.y = CGRectGetMaxY(_backHeadImageView.frame); self.webBrowserView.frame = frame; //更改webBrowserView的frame向下移backHeadImageView的高度,使其可见 6、模态跳转的动画设置 设置模态跳转的动画,系统提供了四种可供选择 DetailViewController *detailVC = [[DetailViewController alloc]init]; //UIModalTransitionStyleFlipHorizontal 翻转 //UIModalTransitionStyleCoverVertical 底部滑出 //UIModalTransitionStyleCrossDissolve 渐显 //UIModalTransitionStylePartialCurl 翻页 detailVC.modalTransitionStyle = UIModalTransitionStylePartialCurl; [self presentViewController:detailVC animated:YES completion:nil]; 7、图片处理只拿到图片的一部分 UIImage *image = [UIImage imageNamed:filename]; CGImageRef imageRef = image.CGImage; CGRect rect = CGRectMake(origin.x, origin.y ,size.width, size.height); //这里的宽高是相对于图片的真实大小 //比如你的图片是400x400的那么(0,0,400,400)就是图片的全尺寸,想取哪一部分就设置相应坐标即可 CGImageRef imageRefRect = CGImageCreateWithImageInRect(imageRef, rect); UIImage *imageRect = [[UIImage alloc] initWithCGImage:imageRefRect]; 8、给UIView设置图片 UIImage *image = [UIImage imageNamed:@"playing"]; _layerView.layer.contents = (__bridge id)image.CGImage; _layerView.layer.contentsCenter = CGRectMake(0.25, 0.25, 0.5, 0.5); //同样可以设置显示的图片范围 //不过此处略有不同,这里的四个值均为0-1之间;对应的依然是写x,y,widt,height