10 Practical Tips for iOS (There are always things you don’t know and things you will use)

10 Practical Tips for iOS (There are always things you don’t know and things you will use)

[[184792]]

During the development process, we will always encounter various small problems, some of which are not very easy to solve. Here I will summarize the various small problems I encountered during development and my solutions. I will not mention the more common ones, but mainly talk about some that you may not know (of course, it is also possible that you know them all, so you don't need to read on)

1. Local rounded corners of controls

Have you ever encountered this problem: a button or label only has two rounded corners on the right, or only one rounded corner? What should you do? This is where layer masks come in handy.

  1. CGRect rect = CGRectMake(0, 0, 100, 50);
  2.  
  3. CGSize radio = CGSizeMake(5, 5); //Rounded corner size
  4.  
  5. UIRectCorner corner = UIRectCornerTopLeft|UIRectCornerTopRight; //This is the rounded corner position
  6.  
  7. UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corner cornerRadii:radio];
  8.  
  9. CAShapeLayer *masklayer = [[CAShapeLayer alloc]init];//Create shapelayer
  10.  
  11. masklayer.frame = button.bounds;
  12.  
  13. masklayer.path = path.CGPath; //Set the path
  14.  
  15. button.layer.mask = masklayer;

For example, button, other controls inherited from UIView can

2. Transparency issue of navigationBar

If you only set the alpha of the navigationBar to 0, it is equivalent to hiding the navigationBar. As we all know, if the alpha of the parent view is set to 0, all the child views will be transparent. Then the title and the left and right buttons of the corresponding navigationBar will disappear. This obviously does not achieve the effect we require.

(1) If you only want the navigationBar to be transparent, and both the button and title are available, you can use the following method:

  1. [self.navigationController.navigationBar setBackgroundImage:[UIImage new]
  2.  
  3. forBarMetrics:UIBarMetricsDefault]; //Set an empty background image for navigationBar to achieve transparency, and the title buttons are all

If you are careful, you will find that there is a line as shown below:

This requires us to do further processing and remove the line, as follows:

  1. self.navigationController.navigationBar.shadowImage = [UIImage new];
  2.  
  3. //In fact, this line is also controlled by image. Just set it to empty

(2) If you want to achieve the effect of changing from transparent to opaque based on the pull-down distance, then the above method will not be enough, so we need to use another method.

  1. //navigationBar is a composite view, which is composed of many controls, so we can start from its internal
  2.  
  3. [[self.navigationController.navigationBar subviews] objectAtIndex:0].alpha = 0; //Here you can set alpha according to the offset of scrollView to achieve a gradual transparent effect

3. Globally set the style of the navigationBar title and the barItem title style

  1. //UIColorWithHexRGB() This method is self-defined, here you just need to give a color
  2.  
  3. [[UINavigationBar appearance] setBarTintColor:UIColorWithHexRGB(0xfefefe)];
  4.  
  5.  
  6. [[UINavigationBar appearance] setTitleTextAttributes:@{NSFontAttributeName:[UIFontboldSystemFontOfSize:18],NSForegroundColorAttributeName:UIColorWithHexRGB(0xfe6d27)}];  
  7.   
  8.  
  9. [[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName : [UIFontboldSystemFontOfSize:10],NSForegroundColorAttributeName : UIColorWithHexRGB(0x666666)}forState:UIControlStateNormal];
  10.  
  11.   
  12.   [[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSiz

4. Transition of hiding and displaying navigationBar

I believe you must have encountered this situation during use: one page hides the navigationBar, while the other does not. When the two pages are pushed or popped, especially when there is a side swipe gesture to return, if no processing is done, the navigationBar position will be empty when sliding back, and a black screen or the view below will be displayed directly, which is ugly. In this case, we need to add a transition animation to hide or show the navigationBar:

Implement the viewWillAppear method on the page that will appear after returning. Set it to YES if you need to hide it, and set it to NO if you need to show it.

  1. - (void)viewWillAppear:(BOOL)animated{
  2.  
  3. [super viewWillAppear:animated];
  4.  
  5. [self.navigationController setNavigationBarHidden: NO animated:YES];
  6.  
  7. }

5. Slide back

The side-slide back gesture of iOS has a good operating experience. An application that does not support the side-slide back is definitely not a good application. However, during the development process, when the back button is customized, or some webView, tableView and other pages, the side-slide back gesture fails. At this time, we need to set it up. You can negotiate the following code in the base class:

  1. if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
  2.  
  3. //Need to follow the gesture's delegate self.navigationController.interactivePopGestureRecognizer.delegate = self;
  4.  
  5. self.navigationController.interactivePopGestureRecognizer.enabled = YES;
  6.  
  7. }

Problem: When you return to the topmost controller of the navigationController and swipe sideways again, if you click on an operation that pushes a page, you will find that it is stuck and takes a long time to respond.

This is because the gesture is still valid in the top-level Controller, but after sliding, you can't find the page you returned to. This causes the software to freeze and freeze, so you need to disable this gesture in the rootViewController. Set the following to NO

self.navigationController.interactivePopGestureRecognizer.enabled = YES;

Of course, you can also use a third-party library, which is very well written. It expands the system's side-swipe back gesture. Instead of sliding from the edge, you can just swipe right to return. Most importantly, it only needs to be added to the project, and it can be implemented without a single line of code. Attached is the github URL

https://github.com/forkingdog/FDFullscreenPopGesture

6. Add a header view to webView

WebView is a composite view, which contains a scrollView, which contains a UIWebBrowserView (responsible for displaying the content of WebView)

  1. UIView *webBrowserView = self.webView.scrollView.subviews[0];//Get the webBrowserView of webView
  2.  
  3. self.backHeadImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth,kScreenWidth*2/3.0)];
  4.  
  5. [_backHeadImageView sd_setImageWithURL:[NSURL URLWithString:self.imageUrl] placeholderImage:[UIImageimageNamed:@ "placeholderImage" ]];
  6.  
  7. [self.webView insertSubview:_backHeadImageView belowSubview:self.webView.scrollView];
  8.  
  9. //Insert backHeadImageView below the scrollView of webView
  10.  
  11. CGRect frame = self.webBrowserView.frame;
  12.  
  13. frame.origin.y = CGRectGetMaxY(_backHeadImageView.frame);
  14.  
  15. self.webBrowserView.frame = frame;
  16.  
  17. //Change the frame of webBrowserView and move the backHeadImageView down to make it visible

7. Animation settings for modal jump

Set the animation of modal jump. The system provides four options

  1. DetailViewController *detailVC = [[DetailViewController alloc]init];
  2.  
  3. //UIModalTransitionStyleFlipHorizontal flip
  4.  
  5. //UIModalTransitionStyleCoverVertical slides out from the bottom
  6.  
  7. //UIModalTransitionStyleCrossDissolve gradually appears
  8.  
  9. //UIModalTransitionStylePartialCurl page turning
  10.  
  11. detailVC.modalTransitionStyle = UIModalTransitionStylePartialCurl;
  12.  
  13. [self presentViewController:detailVC animated:YES completion:nil];

8. Image processing only gets part of the image

  1. UIImage *image = [UIImage imageNamed:filename];
  2.  
  3. CGImageRef imageRef = image.CGImage;
  4.  
  5. CGRect rect = CGRectMake(origin.x, origin.y, size .width, size .height);
  6.  
  7. //The width and height here are relative to the actual size of the image
  8.  
  9. //For example, if your image is 400x400, then (0, 0, 400, 400) is the full size of the image. If you want to take a part, just set the corresponding coordinates.
  10.  
  11. CGImageRef imageRefRect = CGImageCreateWithImageInRect(imageRef, rect);
  12.  
  13. UIImage *imageRect = [[UIImage alloc] initWithCGImage:imageRefRect];

9. Set the image for UIView

  1. UIImage *image = [UIImage imageNamed:@ "playing" ];
  2.  
  3. _layerView.layer.contents = (__bridge id)image.CGImage;
  4.  
  5. _layerView.layer.contentsCenter = CGRectMake(0.25, 0.25, 0.5, 0.5);
  6.  
  7. //You can also set the displayed image range
  8.  
  9. //However, this is slightly different. The four values ​​here are all between 0 and 1; the corresponding values ​​are still x, y, width, height

10. Add simple animation to the cell of TableView or CollectionView

Just animate the cell to be displayed in the willDisplayCell method:

  1. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath{
  2.  
  3. NSArray *array = tableView.indexPathsForVisibleRows;
  4.  
  5. NSIndexPath *firstIndexPath = array[0];
  6.  
  7.   
  8.  
  9. //Set anchorPoint
  10.  
  11. cell.layer.anchorPoint = CGPointMake(0, 0.5);
  12.  
  13. //To prevent the cell view from moving, put the cell back to its original position
  14.  
  15. cell.layer.position = CGPointMake(0, cell.layer.position.y);
  16.  
  17.   
  18.  
  19. //Set the cell to rotate 90 degrees along the z-axis, note that it is in radians
  20.  
  21. if (firstIndexPath.row < indexPath.row) {
  22.  
  23. cell.layer.transform = CATransform3DMakeRotation(M_PI_2, 0, 0, 1.0);
  24.  
  25. } else {
  26.  
  27. cell.layer.transform = CATransform3DMakeRotation(- M_PI_2, 0, 0, 1.0);
  28.  
  29. }
  30.  
  31.   
  32.  
  33. cell.alpha = 0.0;
  34.  
  35.   
  36.  
  37. [UIView animateWithDuration:1 animations:^{
  38.  
  39. cell.layer.transform = CATransform3DIdentity;
  40.  
  41. cell.alpha = 1.0;
  42.  
  43. }];
  44.  
  45. }
  46.  
  47. - (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cellforItemAtIndexPath:(NSIndexPath *)indexPath{
  48.  
  49.   
  50.  
  51. if (indexPath.row % 2 != 0) {
  52.  
  53. cell.transform = CGAffineTransformTranslate(cell.transform, kScreenWidth/2, 0);
  54.  
  55. } else {
  56.  
  57. cell.transform = CGAffineTransformTranslate(cell.transform, -kScreenWidth/2, 0);
  58.  
  59. }
  60.  
  61. cell.alpha = 0.0;
  62.  
  63. [UIView animateWithDuration:0.7 animations:^{
  64.  
  65. cell.transform = CGAffineTransformIdentity;
  66.  
  67. cell.alpha = 1.0;
  68.  
  69. } completion:^(BOOL finished) {
  70.  
  71.   
  72.  
  73. }];
  74.  
  75. }

<<:  Lock the CPU frequency of Android devices

>>:  iOS Advanced——Block

Recommend

How much does it cost to customize the Yongxin Musical Instruments Mini Program?

How much does it cost to customize the Yongxin Mu...

Rose gold iPhone is not exclusive to girls, pink may become a new choice for men

According to the Wall Street Journal, more and mo...

What should Sharp learn from Japanese rice cooker and toilet seat companies?

Following the bad news of heavy debt, selling bui...

User targeting method for Toutiao advertising!

Those of us who do marketing promotion know that ...

How did a good lion specimen become an Internet celebrity?

In the Shanghai Natural History Museum, you can s...

How to revive an old Douyin account? Can the old Douyin account be restored?

Every day, many Douyin video users register new a...

Changlu Teacher Chip Conversion Ultimate Edition

Introduction to the Ultimate Edition of Changlu T...

Why does 12306 crash from time to time?

01 2019 is about to pass. Have you bought the tra...

How to quickly attract fans through local WeChat public account promotion?

We know that having a local WeChat public account...

How many steps are needed to make a leaf?

When you step on the fallen leaves on the road an...

Tencent architect teaches you how to write Android specification documents

Preface A qualified code should not only realize ...