[置顶] UIView、table、tabbar、Navigation等小功能集锦

一、禁止横屏旋转

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)。


你可能感兴趣的:([置顶] UIView、table、tabbar、Navigation等小功能集锦)