Pitfalls encountered in iOS development

Preface

I have encountered many problems and stepped on many pits since I have been doing iOS development for so long. I never thought about writing this article, but in view of the following three points, I decided to remember what I encountered and can still remember Make a record of the problem.

  • Every time the problem is solved, I feel full of accomplishment, but when I encounter it again next time, I am embarrassed again. I seem to have seen this problem, but I just don't remember how to solve it, so I can record it for later reference.

  • In every interview, the interviewer always asks me, what problems have you encountered during development and how did you solve them? There are a lot of questions, but you want me to narrate, I...I...I can't remember it for a while, so confused???? (The interviewer thought: a look Just a fake resume with no work experience), wronged.

  • Provide a reference for developers who encounter the same problem

The problems encountered and the pits that have been stepped on

1. When the phone is recording or calling, the app interface moves down as a whole

When your iPhone is recording or calling, you will find that the entire interface is moved down by 20 pixels when you open the app. This problem may not have much impact on scrollView, tableView and other views that can be scrolled vertically, but for those views that cannot be scrolled. , It is possible that the bottom is covered by 20 pixels

The author checked the information on the Internet and tried many methods. This problem still exists. Finally, the author had to use a big trick to solve it. What is a big trick? Very simple, the location size is hard to write. The navigation controller in the project is customized. Add the following code to the customized class and it will be OK.

2. Clicking on the bottom of the navigation bar will also trigger the click event of the navigation bar button

As shown in the figure, every time I want to select the picture in the upper right corner, the result always triggers the "cancel" event. It is clear that I did not click on the navigation bar. The cause of the problem is not clear. The solution is to create a category of UINavigationBar and rewrite the hitTest method.

If the touch point is not on it, do not let it receive user interaction, and then use the system's event processing method to handle it

3. Limit character length when inputting Chinese

Everyone knows how to limit the length of letters, numbers, symbols and other non-Chinese characters, but if it is a Chinese input method, this is more painful. For example, if the length is limited to 5, input "I love to write code", when the two words "I love to write" have been input, and then input "code", the following situation will appear

Can't write in at all, why is this? Because the input pinyin is also calculated within the length of the string, the input cannot be continued after "da" is input. The following solutions can be used to solve the problem.

4.修改导航栏颜色

这个问题对有经验的开发者来说不是问题,但是当年笔者刚入行时,确实被这个问题折腾了好久。比如想设置导航栏的颜色为蓝色

self.navigationController.navigationBar.backgroundColor = [UIColor blueColor];

运行,然后......这特么是蓝色?笔者确定自己不是色盲

后来才知道,设置导航栏的背景色,需要设置barTintColor而不是backgroundColor

self.navigationController.navigationBar.barTintColor = [UIColor blueColor];

5.界面莫名其妙下移

这个问题跟上面的问题一样,都是比较基础的问题,有经验的人都知道如何解决,但是笔者曾经也被折磨过

为什么下面textView的内容不是从最上面开始的,而且还不能滚上去,当初真的被这种情况折腾的死去活来的,改frame,改约束,都没效果。后来才知道,当有导航控制器时,系统会默认将第一个添加的scrollView或其子类视图的内容下移64个像素,下面的textView是先添加的,所以其内容被下移了,而上面的却没有,解决方法是在控制器里加上下面这句代码

self.automaticallyAdjustsScrollViewInsets = NO;

6.GCD组函数

笔者在开发中遇到过这样一个功能,某个界面列表上面是企业,下面是联系人,而且获取企业列表与联系人列表的接口不是同一个,必须等企业跟联系人列表数据都获取完毕后才能刷新,于是就用到了GCD组函数

然而问题来了,[[IBOSServer shared]...]这玩意儿是封装好的,并且是个异步函数,大家应该都知道,异步函数不会阻塞线程,不用等里面的内容执行完就返回了,这就导致数据还没拿到,就开始执行notify里面的代码了。后来笔者发现,原来GCD组函数还有一种用法,完美解决问题

7.UISearchController

iOS8才出来的,用来替换UISearchBar + UISearchDisplayController的组合方式,项目中有用到搜索功能,于是笔者就尝试着用了一下UISearchController,将搜索框添加到了tableView的tableHeaderView上。然而当点击搜索框准备搜索时,神奇的事情发生了

搜索框居然飞到九霄云外去了,好难过,这不是我要的那种结果,结果.....
怎么解决呢?在控制器里加上下面这句代码就OK了,至于为什么,呵呵,不知道

self.definesPresentationContext = YES;

8.自定义相机照片截取

这个问题说难不难,说简单也不简单,笔者当时确实花了很长一段时间才弄清楚,但是当问题解决后,感觉这特么也不复杂啊,之前怎么就没整明白呢

问题是这样的,在使用自定义相机拍完照后,需要对图片的指定区域(蓝色方框内的区域)进行截取然后上传,这里存在两个问题。

拍摄时你所看到的区域,跟实际拍出来的区域是不一样的

上图就是实际拍出来的图片,明显比拍摄时看到的图片要长,不过宽度是一样的

照片的坐标系与我们平时开发所使用的坐标系是不一样的,开发中所使用的视图坐标系是以左上角为原点的,横向为x轴,纵向为y轴,而拍摄出来的照片是以右上角为原点的,横向为y轴,纵向为x轴。

我们先来看一幅图

由于拍摄时所看到视图宽高比与实际拍摄出来的图片的宽高比并不一致,所以导致图片经过等比例缩放到宽度一样时,高度有一部分超出了我们的可视范围,首先,我们要计算出超出部分的高度

//_cameraView为相机视图,即拍摄时看到图片的区域

CGFloat scale = image.size.width / _cameraView.width;//计算出缩放的比例

CGFloat showImageH = image.size.height / scale;//缩放后图片的高度

CGFloat offsetY = (showImageH-_cameraView.height) * 0.5;//The upper and lower excess parts are equal, so *0.5 gives the above excess part

After calculating the height of the excess part, we are going to calculate the coordinates of the blue box (_circleView, a child control of _cameraView)

x: (offsetY+_circleView.y) * scale, don’t forget to multiply by the zoom ratio

y: _circleView.x * scale, because circleView is centered horizontally in the parent control, the x value of _circleView in the parent control multiplied by the zoom ratio is the y value in the picture coordinate system

width:_circleView.height scale

height:_circleView.width scale

Then it is ok to capture the picture according to the calculated frame

CGRect rect = CGRectMake(x, y, width,height);

CGImageRef tailorImageRef = CGImageCreateWithImageInRect(photo.CGImage, rect);

UIImage *tailorImage = [UIImage imageWithCGImage:tailorImageRef];

Guess you like

Origin blog.csdn.net/wu347771769/article/details/56277381