当前位置: 移动技术网 > 移动技术>移动开发>IOS > IOS 开发过程中问题汇总

IOS 开发过程中问题汇总

2018年10月25日  | 移动技术网移动技术  | 我要评论

1-调用打电话方法

1> 直接跳转到打电话页面 __拨打完电话回不到原来的应用,会停留在通讯录里,而且是直接拨打,不弹出提示

[[uiapplication sharedapplication] openurl:[nsurl urlwithstring:[nsstring stringwithformat:@“tel://%@",@"4001588168"]]];

2> 自动弹出提示选择后跳转到打电话页面 _ 会回去到原来的程序里(注意这里的telprompt),也会弹出提示 (可能会被拒)

nsstring *telnumber = [nsstring stringwithformat:@"telprompt://%@",self.telephonenum];

3>这种方法,打完电话后还会回到原来的程序,也会弹出提示,推荐这种

nsmutablestring * str=[[nsmutablestring alloc] initwithformat:@"tel:%@",@"186xxxx6979"];
     uiwebview * callwebview = [[uiwebview alloc] init];
        [callwebview loadrequest:[nsurlrequest requestwithurl:[nsurl urlwithstring:str]]];

2、uibutton 按钮上得字左对齐

titleedgeinsets应该是最好的方法,如果只是简单的左对齐,也可以如下方法。

btn.contenthorizontalalignment = uicontrolcontenthorizontalalignmentleft;

3、把字符串中的图片地址转换到数组中

- (nsarray*)getimgarrfromimgstr:(nsstring*)imgstr
{
    return [imgstr componentsseparatedbystring:@","];
}

4、把视图添加到键盘上

for(uiview *window in [uiapplication sharedapplication].windows) {
        if ([window iskindofclass:nsclassfromstring(@“uiremotekeyboardwindow")]) {
            [window addsubview:_doneinkeyboardbutton];
    }
}

- (void)registerforkeyboardnotifications {
    [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(keyboardwasshown:) name:uikeyboardwillshownotification object:nil];

    [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(keyboardwashidden:) name:uikeyboardwillhidenotification object:nil];
}
- (void) keyboardwasshown:(nsnotification *) notification{
    // nsnotification中的 userinfo字典中包含键盘的位置和大小等信息
    nsdictionary *userinfo = [notification userinfo];
    // uikeyboardanimationdurationuserinfokey 对应键盘弹出的动画时间
    cgfloat animationduration = [[userinfo objectforkey:uikeyboardanimationdurationuserinfokey] floatvalue];
    // uikeyboardanimationcurveuserinfokey 对应键盘弹出的动画类型
    nsinteger animationcurve = [[userinfo objectforkey:uikeyboardanimationcurveuserinfokey] integervalue];
    //数字彩,数字键盘添加“完成”按钮
    if (_doneinkeyboardbutton){

        [uiview beginanimations:nil context:null];
        [uiview setanimationduration:animationduration];//设置添加按钮的动画时间
        [uiview setanimationcurve:(uiviewanimationcurve)animationcurve];//设置添加按钮的动画类型

        //设置自定制按钮的添加位置(这里为数字键盘添加“完成”按钮)
        _doneinkeyboardbutton.transform=cgaffinetransformtranslate(_doneinkeyboardbutton.transform, 0, -53);

        [uiview commitanimations];
    }
}

- (void) keyboardwashidden:(nsnotification *)notification {
    // nsnotification中的 userinfo字典中包含键盘的位置和大小等信息
    nsdictionary *userinfo = [notification userinfo];
    // uikeyboardanimationdurationuserinfokey 对应键盘收起的动画时间
    cgfloat animationduration = [[userinfo objectforkey:uikeyboardanimationdurationuserinfokey] floatvalue]/3;

    if (_doneinkeyboardbutton.superview)
    {
        [uiview animatewithduration:animationduration animations:^{
            //动画内容,将自定制按钮移回初始位置
            _doneinkeyboardbutton.transform=cgaffinetransformidentity;
        } completion:^(bool finished) {
            //动画结束后移除自定制的按钮
            [_doneinkeyboardbutton removefromsuperview];
        }];
    }
}
- (void)configdoneinkeyboardbutton {
    cgfloat screenheight = [uiscreen mainscreen].bounds.size.height;
    //初始化
    if (_doneinkeyboardbutton == nil)
    {
        uibutton *doneinkeyboardbutton = [uibutton buttonwithtype:uibuttontypecustom];
        [doneinkeyboardbutton settitle:@"·" forstate:uicontrolstatenormal];
        [doneinkeyboardbutton settitlecolor:[uicolor blackcolor] forstate:uicontrolstatenormal];
        doneinkeyboardbutton.backgroundcolor = [uicolor clearcolor];
        doneinkeyboardbutton.frame = cgrectmake(0, screenheight, (kscreenw - 2)/3, 53);
        doneinkeyboardbutton.titlelabel.font = [uifont systemfontofsize:35];
        doneinkeyboardbutton.adjustsimagewhenhighlighted = no;
        [doneinkeyboardbutton addtarget:self action:@selector(finishaction) forcontrolevents:uicontroleventtouchupinside];
        _doneinkeyboardbutton = doneinkeyboardbutton;
    }
    //每次必须从新设定“小数点”按钮的初始化坐标位置
    _doneinkeyboardbutton.frame = cgrectmake(0, screenheight, (kscreenw - 2)/3, 53);
    //由于ios8下,键盘所在的window视图还没有初始化完成,调用在下一次 runloop 下获得键盘所在的window视图
    [self performselector:@selector(adddonebutton) withobject:nil afterdelay:0.0f];
}
#pragma mark - 添加小数点按钮
- (void)adddonebutton{
    //获得键盘所在的window视图,直接加到window上
    for(uiview *window in [uiapplication sharedapplication].windows)
    {
        if([window iskindofclass:nsclassfromstring(@"uiremotekeyboardwindow")])
        {
            [window addsubview:_doneinkeyboardbutton];
        }
    }
}
#pragma mark - 点击“小数点”按钮事件
-(void)finishaction {
    _cznumtf.text = [nsstring stringwithformat:@"%@.",_cznumtf.text];
}

5、uilabel的自适应

5.1、uilabel的宽度自适应

adjustsfontsizetofitwidth

5.2、uilabel的高度自适应

boundingrectwithsize: options:  attributes: context:
sizethatfits:

6、uitextfield 的 placeholder的颜色改变

 //第一种
uicolor *color = [uicolor whitecolor];  
_username.attributedplaceholder = [[nsattributedstring alloc] initwithstring:@"用户名" attributes:@{nsforegroundcolorattributename: color}];  

//第二种   
[_username setvalue:[uicolor whitecolor] forkeypath:@"_placeholderlabel.textcolor"]; 

7、保存图片到相册

- (ibaction)saveimagetoalbum:(id)sender {
    uiimagewritetosavedphotosalbum(self.imageview.image, self, @selector(imagesavedtophotosalbum:didfinishsavingwitherror:contextinfo:), nil);
}
- (void)imagesavedtophotosalbum:(uiimage *)image didfinishsavingwitherror:(nserror *)error contextinfo:(void *)contextinfo {
    nsstring *message = @"呵呵";
    if (!error) {
        message = @"成功保存到相册";
    }else
    {
        message = [error description];
    }
    nslog(@"message is %@",message);
}

8、edgesforextendedlayout

edgesforextendedlayout是一个类型为uiextendededge的属性,指定边缘要延伸的方向。

因为ios7鼓励全屏布局,它的默认值很自然地是uirectedgeall,四周边缘均延伸,就是说,如果即使视图中上有navigationbar,下有tabbar,那么视图仍会延伸覆盖到四周的区域。

9、 automaticallyadjustsscrollviewinsets

当 automaticallyadjustsscrollviewinsets 为 no 时,tableview 是从屏幕的最上边开始,也就是被 导航栏 & 状态栏覆盖。 当 automaticallyadjustsscrollviewinsets 为 yes 时,也是默认行为,表现就比较正常了,和edgesforextendedlayout = uirectedgenone 有啥区别? 不注意可能很难觉察, automaticallyadjustsscrollviewinsets 为yes 时,tableview 上下滑动时,是可以穿过导航栏&状态栏的,在他们下面有淡淡的浅浅红色

10、extendedlayoutincludesopaquebars

首先看下官方解释,默认 no, 但是bar 的默认属性是透明的。。。也就是说只有在不透明下才有用但是,测试结果很软肋,基本区别不大。。。但是对于解决一些bug 是还是起作用的,比如说searchbar的跳动问题,详情见:, 其他uitableview,uiscrollview 位置的问题多数和这3属性相关。

11、正则表达式的用法

//电话号码
nsstring *telpattern = @"\\d{3,4}-)\\d{7,8}$";
//手机号码
nsstring *phonepattern = @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";
//qq号码
nsstring *qqpattern = @"^[1-9]\\d[4,10]$";
//邮箱地址
nsstring *mailpattern = @"^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
//url网址
nsstring *urlpattern = @"((http[s]{0,1}|ftp)://[a-za-z0-9\\.\\-]+\\.([a-za-z]{2,4})(:\\d+)?(/[a-za-z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)|(www.[a-za-z0-9\\.\\-]+\\.([a-za-z]{2,4})(:\\d+)?(/[a-za-z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)";
//身份证号码
nsstring *idcardpattern = @"\\d{14}[[0-9],0-9xx]";
//数字和字母
nsstring *numcharacterpattern = @"^[a-za-z0-9]+$";
//整数和小数
nsstring *allnumpattern = @"^[0-9]+([.]{0,1}[0-9]+){0,1}$";
//汉字
nsstring *hanspattern = @"^[\u4e00-\u9fa5]{0,}$";
//ip地址
nsstring *ippattern = @“((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)";

nsstring *pattern = [nsstring stringwithformat:@“%@|%@|%@|%@|%@|%@|%@|%@|%@|%@",telpattern,phonepattern,qqpattern,mailpattern,urlpattern,idcardpattern,numcharacterpattern,allnumpattern,hanspattern,ippattern];

nsregularexpression *reges = [[nsregularexpression alloc] initwithpattern:pattern options:0 error:nil];

nsstring *str = @“————“;

nsarray *results = [reges matchesinstring:str options:0 range:nsmakerange(0, str.length)];

for (nstextcheckingresult *result in results) {
       nslog(@"%@ %@",nsstringfromrange(result.range),[str substringwithrange:result.range]);
}

12、数组排序

nsarray *array = [nsarray array];
    [array sortedarrayusingcomparator:^nscomparisonresult(id obj1, id obj2) {
        if (obj1 > obj2) {
            return nsorderedascending;//升序
        }
        return nsordereddescending;//降序
    }];

13、iphone 各版本的尺寸,分辨率

3.5英寸——iphone5 :        320 x 480  640 x 960
4.0英寸——iphone5s :       320 x 568  640 x 1136
4.7英寸——iphone6 :        375 x 667  750 x 1334
5.5英寸——iphone6 plus :   414 x 736  1080 x 1920

14、将项目的.svn全部删除

打开终端,进入项目所在的文件夹:使用命令find . -type d -name ".svn" |xargs rm -rvf就可将项目的.svn全部删除;

15、根据value快速查找数组中的模型(谓词)

nspredicate *predicate = [nspredicate predicatewithformat:@"key = %@",value];
        nsarray *array = [modelarray filteredarrayusingpredicate:predicate];

16、图片的拉伸

image= [image stretchableimagewithleftcapwidth:image.size.width * 0.5 topcapheight:image.size.height * 0.5];

17、修改字间距

nsmutableattributedstring *attr = [[nsmutableattributedstring alloc] initwithstring:textfield.text];
    [attr addattribute:nskernattributename value:[nsnumber numberwithfloat:4.0] range:nsmakerange(0, textfield.text.length)];
    textfield.attributedtext = attr;

18、研究富文本attributedstring

1> nsfontattributename    设置字体属性,默认值:字体:helvetica(neue) 字号:12
2> nsforegroundcolorattributename   设置字体颜色,取值为 uicolor对象,默认值为黑色
3> nsbackgroundcolorattributename     设置字体所在区域背景颜色,取值为 uicolor对象,默认值为nil, 透明色
4> nsligatureattributename  设置连体属性,取值为nsnumber 对象(整数),0 表示没有连体字符,1 表示使用默认的连体字符
5> nskernattributename  设定字符间距,取值为 nsnumber 对象(整数),正值间距加宽,负值间距变窄
6> nsstrikethroughstyleattributename  设置删除线,取值为 nsnumber 对象(整数)
7> nsstrikethroughcolorattributename  设置删除线颜色,取值为 uicolor 对象,默认值为黑色——设置中划线 [nsnumber numberwithinteger:nsunderlinestylesingle]
8> nsunderlinestyleattributename    设置下划线,取值为 nsnumber 对象(整数),枚举常量 nsunderlinestyle中的值,与删除线类似
9> nsunderlinecolorattributename   设置下划线颜色,取值为 uicolor 对象,默认值为黑色
10> nsstrokewidthattributename    设置笔画宽度,取值为 nsnumber 对象(整数),负值填充效果,正值中空效果
11> nsstrokecolorattributename  填充部分颜色,不是字体颜色,取值为 uicolor 对象
12> nsshadowattributename    设置阴影属性,取值为 nsshadow 对象
13> nstexteffectattributename  设置文本特殊效果,取值为 nsstring 对象,目前只有图版印刷效果可用:
14> nsbaselineoffsetattributename   设置基线偏移值,取值为 nsnumber (float),正值上偏,负值下偏
15> nsobliquenessattributename   设置字形倾斜度,取值为 nsnumber (float),正值右倾,负值左倾
16> nsexpansionattributename   设置文本横向拉伸属性,取值为 nsnumber (float),正值横向拉伸文本,负值横向压缩文本
17> nswritingdirectionattributename    设置文字书写方向,从左向右书写或者从右向左书写
18> nsverticalglyphformattributename   设置文字排版方向,取值为 nsnumber 对象(整数),0 表示横排文本,1 表示竖排文本
19> nslinkattributename     设置链接属性,点击后调用浏览器打开指定url地址
20> nsattachmentattributename   设置文本附件,取值为nstextattachment对象,常用于文字图片混排
21> nsparagraphstyleattributename   设置文本段落排版格式,取值为 nsparagraphstyle 对象

19、解决uitableview分割线显示不全,左边有一段缺失

//下面code 可以解决
- (void)viewdidlayoutsubviews {
  if ([self.tableview respondstoselector:@selector(setseparatorinset:)]) {
       [self.tableview setseparatorinset:uiedgeinsetszero];
  }
   if ([self.tableview respondstoselector:@selector(setlayoutmargins:)])  {
       [self.tableview setlayoutmargins:uiedgeinsetszero];
   }
}
- (void)tableview:(uitableview *)tableview willdisplaycell:(uitableviewcell *)cell forrowatindexpath:(nsindexpath *)indexpat{
    if ([cell respondstoselector:@selector(setlayoutmargins:)]) {
      [cell setlayoutmargins:uiedgeinsetszero];
  }
   if ([cell respondstoselector:@selector(setseparatorinset:)]){
       [cell setseparatorinset:uiedgeinsetszero];
   }  
}

20、uitableview的row少时没有数据的cell显示分割线的问题

[tableview settablefooterview:[[uiview alloc] initwithframe:cgrectzero]];

21、uilabel的文字左右对齐

- (void)conversioncharacterinterval:(nsinteger)maxinteger current:(nsstring *)currentstring withlabel:(uilabel *)label
{
    cgrect rect = [[currentstring substringtoindex:1] boundingrectwithsize:cgsizemake(200,label.frame.size.height) options:nsstringdrawinguseslinefragmentorigin |nsstringdrawingusesfontleading
 attributes:@{nsfontattributename: label.font}
 context:nil];
    nsmutableattributedstring *attrstring = [[nsmutableattributedstring alloc] initwithstring:currentstring];
    float strlength = [self getlengthofstring:currentstring];
    [attrstring addattribute:nskernattributename value:@(((maxinteger - strlength) * rect.size.width)/(strlength - 1)) range:nsmakerange(0, strlength)];
    label.attributedtext = attrstring;
}

-  (float)getlengthofstring:(nsstring*)str {
    float strlength = 0;
    char *p = (char *)[str cstringusingencoding:nsunicodestringencoding];
    for (nsinteger i = 0 ; i < [str lengthofbytesusingencoding:nsunicodestringencoding]; i++) {
        if (*p) {
            strlength++;
        }
        p++;
    }
    return strlength/2;
}
//注:中文字符长度1,英文字符及数字长度0.5

22、运行时runtime获取某个类的成员变量

unsigned int count = 0;
    ivar *ivars = class_copyivarlist([uitextfield class], &count);
    for (int i = 0; i < count; i ++) {
        //取出成员变量
        ivar ivar = *(ivars + i);
        //打印成员变量
        nslog(@"%s",ivar_getname(ivar));
    }

23、cell自动计算高度

1、首先的让cell的底部与挨着的子控件底部脱线建立关系;
2、设置估算高度 self.tableview.estimatedrowheight = 70;
3、设置高度自动计算 self.tableview.rowheight = uitableviewautomaticdimension;

24、状态栏的样式

view controller-based status bar appearance 在info.plist里边添加它, value为no,意思是说状态栏的样式更改不是基于控制器,而是由uiapplication决定;如果value=yes 状态栏的样式有控制器决定。

25、导航控制器左边缘右滑动实现返回功能

如果修改了nav的leftbarbuttonitem就没有此功能,如果需要此功能可以设置self.interactivepopgesturerecognizer.delegate = nil;

26、崩溃信息解析查找

先把xxx.app 和 xxx.dsym 两个文件放在一个文件夹里,再根据crash的地址 ,用终端cd 到该文件夹,输入命令 atos -o xxx.app/xxx -arch arm64 0x113313131003 回车即可;

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网