美文网首页
iOS 开发一些方法记录

iOS 开发一些方法记录

作者: developer200 | 来源:发表于2019-11-12 11:23 被阅读0次

UIView设置半透明后不影响其上子控件透明度的方法

self.view.backgroundColor= [[UIColor grayColor] colorWithAlphaComponent:0.5];

通过这种方法设置透明度不会影响加载在其上的控件的透明度

通过 indexPath 获取 Cell

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

包含字符串

[str1 containsString:str2]

iOS 字符串的操作,去掉某一个字符或者替换成其他字符

NSString *strUrl = [urlString stringByReplacingOccurrencesOfString:@" " withString:@""];  去掉空格

NSString *strUrl = [urlString stringByReplacingOccurrencesOfString:@" " withString:@"/"];替换字符

iOS设置UIButton文字显示位置和字体大小、颜色的方法

设置按钮上的自体的大小

[btn setFont: [UIFont systemFontSize: 14.0]]; //这种可以用来设置字体的大小,但是可能会在将来的SDK版本中去除改方法

应该使用btn.titleLabel.font = [UIFont systemFontOfSize: 14.0];

UIButton的title居左对齐 

btn.contentHorizontalAlignment = UIControlContentHorizonAlignmentLeft;

此时文字会紧贴到做边框,我们可以设置

btn.contentEdgeInsets = UIEdgeInsetsMake(0,10, 0, 0);//使文字距离做边框保持10个像素的距离。

UIButton字体颜色

[btn setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];

UITableView

去掉Cell下划线
tableView.separatorStyle =UITableViewCellSeparatorStyleNone;//推荐该方法

取消headerView悬停

tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];

超出隐藏

cell.layer.masksToBounds = YES;

删除所有子视图

 [cell.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

UIButton 按钮边框

[box.actionButton.layer setMasksToBounds:YES]; [box.actionButton.layer setCornerRadius:10.0];//设置矩形四个圆角半径  [box.actionButton.layer setBorderWidth:1.0];//边框宽度 

//设置边框颜色有两种方法:

第一种如下:

 CGColorSpaceRefcolorSpace =CGColorSpaceCreateDeviceRGB();

CGColorRefcolorref =CGColorCreate(colorSpace,(CGFloat[]){0,0,0,1}); 

 [box.actionButton.layer setBorderColor:colorref];//边框颜色 

 //第二种方法如下:

 //button.layer.borderColor=[UIColor grayColor].CGColor;

UILabel 换行

@property(nonatomic)        NSLineBreakMode    lineBreakMode; 

   UILineBreakModeWordWrap = 0, 以单词为单位换行,以单位为单位截断。

   UILineBreakModeCharacterWrap,    以字符为单位换行,以字符为单位截断。 

   UILineBreakModeClip, 以单词为单位换行。以字符为单位截断。

   UILineBreakModeTailTruncation, 以单词为单位换行。无论是单行还是多行,都是末尾有省略号。

   UILineBreakModeMiddleTruncation, 以单词为单位换行。无论是单行还是多行,都是中间有省略号,省略号后面只有2个字符。 

   UILineBreakModeHeadTruncation,  以单词为单位换行。如果是单行,则开始部分有省略号。如果是多行,则中间有省略号,省略号后面有4个字符。

label 中划线

UILabel*strikeLabel=[[UILabel alloc]initWithFrame:(CGRectMake(10,10,50,30))];NSString*textStr=[NSString stringWithFormat:@"%@元",primeCost];//中划线NSDictionary*attribtDic=@{NSStrikethroughStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleSingle]};NSMutableAttributedString*attribtStr=[[NSMutableAttributedString alloc]initWithString:textStr attributes:attribtDic];// 赋值strikeLabel.attributedText=attribtStr;[self.view addSubview:strikeLabel];

label 下划线

UILabel*underlineLabel=[[UILabel alloc]initWithFrame:(CGRectMake(10,10,50,30))];NSString*textStr=[NSString stringWithFormat:@"%@元",primeCost];// 下划线NSDictionary*attribtDic=@{NSUnderlineStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleSingle]};NSMutableAttributedString*attribtStr=[[NSMutableAttributedString alloc]initWithString:textStr attributes:attribtDic];//赋值underlineLabel.attributedText=attribtStr;[self.view addSubview:underlineLabel];


// 1. 创建一个点击事件,点击时触发labelClick方法
UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(popRenzhengView)];

// 2. 将点击事件添加到label上

[cell.goRZLabel addGestureRecognizer:gesture];

//开启用户交互

cell.goRZLabel.userInteractionEnabled = YES;


获取状态栏高度

-(float)mStatusbarHeight{

    //状态栏高度

    return [[UIApplication sharedApplication] statusBarFrame].size.height;

}

获取导航栏高度+状态栏高度

-(float)mNavigationbarHeight{

    //导航栏高度+状态栏高度

    return self.navigationController.navigationBar.frame.size.height + [[UIApplication sharedApplication] statusBarFrame].size.height;

}

获取Tabbar高度

-(float)mTabbarHeight{

    //Tabbar高度

    return self.tabBarController.tabBar.bounds.size.height;

}

   从导航堆栈中删除 viewcontrollers

替换

    NSArray *arr = self.navigationController.viewControllers;

    NSArray*navNewArr = [[NSArrayalloc]initWithObjects:arr[0],arr[1],arr[2],arr[4],nil];

    self.navigationController.viewControllers = navNewArr;

删除

NSMutableArray *navigationarray = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];

[navigationarray removeObjectAtIndex:4];

[navigationarray removeObjectAtIndex:3];

self.navigationController.viewControllers = navigationarray;

iOS13 状态栏

UIView*statusBar;

    if(@available(iOS13.0, *)) {

        UIWindow*keyWindow = [UIApplicationsharedApplication].keyWindow;

        UIView*viewStatusColorBlend = [[UIViewalloc]initWithFrame:keyWindow.windowScene.statusBarManager.statusBarFrame];

        viewStatusColorBlend.backgroundColor= color;

        [keyWindowaddSubview:viewStatusColorBlend];

    }else{

        statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];

        if([statusBarrespondsToSelector:@selector(setBackgroundColor:)]) {

            statusBar.backgroundColor= color;

        }

    }

相关文章

网友评论

      本文标题:iOS 开发一些方法记录

      本文链接:https://www.haomeiwen.com/subject/sbsykctx.html