The pits that have been encountered in iOS development
Objective
iOS development so long time, encountered a lot of difficulties, also stepped on a lot of pits, originally did not want to write this article, but in view of the following three points, the author decided to meet and can recall the problem to make a record.
-
Every time the problem is resolved, full of accomplishment, but the next time again encountered, but also a face confused force. I seem to have seen this problem, but I do not remember how to solve it, the record is convenient for later inspection.
-
Every time the interview, the interviewer will always ask me, you in the development of what problems encountered, how to solve? Problem, a lot of, but you want me to narrate, I ... I...... I can't think of a moment, the embarrassment of the mystery???? (Interviewer thought: A look is a job experience forged CV), wronged AH.
-
Provides a reference for developers who encounter the same problem
The problems they've encountered and the pits they've stepped on.
1. When the phone is recording or calling, the app interface moves down the whole
When your iphone is recording or calling, open the app and you'll notice that the entire interface has moved down 20 pixels, which may not have much effect on ScrollView, TableView, and other views that can be scrolled vertically, but for those views that can't be scrolled, It's possible the bottom is covered. 20 pixels
The author on-line access to information, but also tried a variety of methods, the problem still exists, finally I have to use the big trick to solve, what is the big trick? Very simple, the location of the size to write dead. The navigation controller in the project is customized, and adding the following code to the custom class is OK.
2. Clicking on the bottom of the navigation bar will also trigger the Click event of the Navigation bar button
, each time you want to select the upper right corner of the picture, the results always trigger the "Cancel" event, obviously did not point to the navigation bar above, the cause of the problem is not clear, the solution to create a Uinavigationbar classification, 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 handling method to handle
3. Limit character length when entering Chinese
Limit the length of other non-Chinese characters, such as letters, numbers, symbols, etc. everyone should know the whole, but if it is Chinese input method, this is compared to the egg pain. For example, limit the length to 5, enter "I love to write code", when you have entered the "I love to write" two words, and then enter "code" will appear the following situation
It's not going to go in there. Because the input pinyin is also calculated within the string length, resulting in the input "Da" can not continue to input, can be resolved by the following method
4. Modify the navigation bar color
This problem is not a problem for experienced developers, but when I first entered the line, it was really a long time to toss this problem. For example, to set the navigation bar color to Blue
| 1 |
self.navigationController.navigationBar.backgroundColor = [UIColor blueColor]; |
Run, and then ...???? Is this special blue? I'm sure I'm not color blind.
Later only to know, set the background color of the navigation bar, you need to set bartintcolor instead of BackgroundColor
| 1 |
self.navigationController.navigationBar.barTintColor = [UIColor blueColor]; |
5. The interface inexplicably moves down
This problem is the same as the above problem, is a relatively basic problem, experienced people know how to solve, but I have been tortured
Why the following TextView content is not from the top of the beginning, but also can not roll up, at the beginning of this situation was really frustrating, change frame, change the constraints, have no effect. Later, when there is a navigation controller, the system will default to the first added ScrollView or its sub-Class view of the content of 64 pixels, the following textview is added first, so its content is moved down, but the above is not, the solution is to add the following code in the controller
| 1 |
self.automaticallyAdjustsScrollViewInsets = NO; |
6.GCD Group functions
In the development of the author encountered such a function, a list of interfaces above is the enterprise, the following is the contact, and get the Enterprise list and contact list of the interface is not the same, must wait for the enterprise and contact list data to be completed before the refresh, so it used the GCD group function
However the question came, [[Ibosserver shared] ...] This thing is encapsulated, and is an asynchronous function, we should all know that the asynchronous function does not block the thread, not wait for the contents of the content to return after execution, which leads to the data has not been received, began to execute the code inside the notify. Later I found that the original GCD group function has a use, perfect solution to the problem
7.UISearchController
IOS8 just came out, used to replace Uisearchbar + Uisearchdisplaycontroller combination way, the project useful to search function, so I tried to use a bit of Uisearchcontroller, The search box was added to the TableView Tableheaderview. However, when you click the search box to prepare for the search, something magical happens.
Search box actually flew to Cloud nine, good sad, this is not the kind of result that I want, result ...
How to solve it? Add the following code in the controller is OK, as for why, hehe????, do not know
| 1 |
self.definesPresentationContext = YES; |
8. Custom Camera Photo Capture
This problem is not difficult to say, simple is not simple, I did spend a long time to figure it out, but when the problem solved, feel this special is not complicated ah, before how did not understand it?
The problem is that, after taking photos with a custom camera, you need to intercept and upload the specified area of the picture (the area within the blue box), and there are two questions.
The area you see when you shoot is not the same as the area you actually filmed.
is actually taken out of the picture, obviously more than the picture to see when shooting long, but the width is the same
-
The coordinate system of the photograph is not the same as the coordinate system we normally use for development.
The view coordinate system used in development is the origin of the upper-left corner, the x-axis, and the y-axis vertically, while the photograph taken is the origin of the upper-right corner, the y-axis, and the x-axis vertically.
Let's take a look at the picture first.
Because the aspect ratio of the view is not the same as the aspect ratio of the picture that was actually taken, so the picture is scaled to the width of the image, and the height is partly beyond our visual range, first of all, we want to calculate the height of the excess.
| 1234 |
//_cameraView为相机视图,即拍摄时看到图片的区域CGFloat scale = image.size.width / _cameraView.width;//计算出缩放的比例CGFloat showImageH = image.size.height / scale;//缩放后图片的高度CGFloat offsetY = (showImageH - _cameraView.height) * 0.5;//上下超出的部分是相等的,所以*0.5得出上面超过的部分 |
After calculating the height of the above section, we will calculate the coordinates of the blue box (_circleview,_cameraview).
| 1234 |
x:(offsetY+_circleView.y) * scale,别忘了乘以缩放比例y:_circleView.x * scale,因为circleView在父控件中水平居中,所以_circleView在父控件中的x值乘以缩放比就是在图片坐标系中的y值width:_circleView.height scaleheight:_circleView.width scale |
And then, based on the calculated frame, capture the picture and it's okay.
| 123 |
CGRect rect = CGRectMake(x, y, width,height);CGImageRef tailorImageRef = CGImageCreateWithImageInRect(photo.CGImage, rect);UIImage *tailorImage = [UIImage imageWithCGImage:tailorImageRef]; |
Conclusion
Encountered the pit, far more than the above, but some do not remember, and later met to fill it up!
The pits that have been encountered in iOS development