You must know: 22 weird tricks for iOS development

You must know: 22 weird tricks for iOS development

Combining his own practical development experience, he summarized 22 small tips for iOS development, and used a very cheerful tone to easily solve various difficult problems encountered in the development process. Just reading it makes you laugh.

1. What should I do if TableView does not display cells with no content?

Similar to Figure 1, I don't want the blanks below to show up. It's very simple, add "self.tableView.tableFooterView = [[UIView alloc] init];" and it works. After adding this sentence, it becomes what Figure 2 looks like.

2. What should I do if the left swipe back gesture of the customized leftBarbuttonItem fails?

  1. self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]
  2. initWithImage:img
  3. style:UIBarButtonItemStylePlain
  4. target:self
  5. action: @selector (onBack:)];
  6. self.navigationController.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>)self;

3. What should I do if ScrollView cannot be scrolled to the top in viewController for unknown reasons?

  1. self.automaticallyAdjustsScrollViewInsets = NO;

4. I am so annoyed writing keyboard events that I want to throw the keyboard. What should I do?

Buy a solid keyboard;
After using IQKeyboardManager (searchable on GitHub), my waist and legs no longer hurt.

5. Why is my app always not running smoothly? What’s the problem?

As shown in the figure:

This artifact is called: KMCGeigerCounter, go to GitHub and copy it.

6. How to adjust the position of separaLine without creating a new Cell?

  1. _myTableView.separatorInset = UIEdgeInsetsMake( 0 , 100 , 0 , 0 );

7. How to close the keyboard by clicking on self.view? Do I need to add a tapGestures?

  1. - ( void )touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  2. {
  3. [self.view endEditing:YES];
  4. }

8. How to set a default background image for each ViewController?

Use the base class, boy.

9. I want to change the layoutAttributes added in the xib in code, but how can I find it in code?

Pull your constraints like you pull a Button, NSLayoutAttribute can also pull lines.

10. How to hide the navigationbar when scrolling like Safari?

  1. navigationController.hidesBarsOnSwipe = Yes

11. The title of the back button on the navigation bar is so annoying, how can I make it disappear?

  1. [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake( 0 , - 60 )
  2. forBarMetrics:UIBarMetricsDefault];

12. CoreData is annoying to use. What should I do if the syntax is long and boring?

MagicRecord

13. How does CollectionView implement a hovering header like tableview?

CSStickyHeaderFlowLayout

14. Can I use only one pan gesture to replace all directions of UISwipegesture?

  1. - ( void )pan:(UIPanGestureRecognizer *)sender
  2. {
  3. typedef NS_ENUM(NSUInteger, UIPanGestureRecognizerDirection) {
  4. UIPanGestureRecognizerDirectionUndefined,
  5. UIPanGestureRecognizerDirectionUp,
  6. UIPanGestureRecognizerDirectionDown,
  7. UIPanGestureRecognizerDirectionLeft,
  8. UIPanGestureRecognizerDirectionRight
  9. };
  10. static UIPanGestureRecognizerDirection direction = UIPanGestureRecognizerDirectionUndefined;
  11. switch (sender.state) {
  12. case UIGestureRecognizerStateBegan: {
  13. if (direction == UIPanGestureRecognizerDirectionUndefined) {
  14. CGPoint velocity = [sender velocityInView:recognizer.view];
  15. BOOL isVerticalGesture = fabs(velocity.y) > fabs(velocity.x);
  16. if (isVerticalGesture) {
  17. if (velocity.y > 0 ) {
  18. direction = UIPanGestureRecognizerDirectionDown;
  19. } else {
  20. direction = UIPanGestureRecognizerDirectionUp;
  21. }
  22. }
  23. else {
  24. if (velocity.x > 0 ) {
  25. direction = UIPanGestureRecognizerDirectionRight;
  26. } else {
  27. direction = UIPanGestureRecognizerDirectionLeft;
  28. }
  29. }
  30. }
  31. break ;
  32. }
  33. case UIGestureRecognizerStateChanged: {
  34. switch (direction) {
  35. case UIPanGestureRecognizerDirectionUp: {
  36. [self handleUpwardsGesture:sender];
  37. break ;
  38. }
  39. case UIPanGestureRecognizerDirectionDown: {
  40. [self handleDownwardsGesture:sender];
  41. break ;
  42. }
  43. case UIPanGestureRecognizerDirectionLeft: {
  44. [self handleLeftGesture:sender];
  45. break ;
  46. }
  47. case UIPanGestureRecognizerDirectionRight: {
  48. [self handleRightGesture:sender];
  49. break ;
  50. }
  51. default : {
  52. break ;
  53. }
  54. }
  55. break ;
  56. }
  57. case UIGestureRecognizerStateEnded: {
  58. direction = UIPanGestureRecognizerDirectionUndefined;
  59. break ;
  60. }
  61. default :
  62. break ;
  63. }
  64. }

15. How can I keep the image from being deformed when stretching it?

Method 1:

  1. UIImage *image = [[UIImage imageNamed:@ "xxx" ] stretchableImageWithLeftCapWidth: 10 topCapHeight: 10 ];

Note: Some developers have reminded that this method has been deprecated and is now called resizableImageWithCapInsets.

Method 2, as shown in the figure:

16. Why is there so much lag when playing GIF? Is there a better library?

FLAnimatedImage by FlipBoard is perfect for you.

17. How to add pull-to-refresh in one sentence?

Using SVPullToRefresh library:

  1. [tableView addPullToRefreshWithActionHandler:^{
  2. // prepend data to dataSource, insert cells at top of table view  
  3. // call [tableView.pullToRefreshView stopAnimating] when done  
  4. } position:SVPullToRefreshPositionBottom];

18. How to change the color of the small check mark in the Cell in the tableview to another color?

  1. _mTableView.tintColor = [UIColor redColor];

19. Originally my statusbar was lightcontent, but using UIImagePickerController caused the style of my statusbar to turn black. What should I do?

  1. - ( void )navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
  2. {
  3. [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
  4. }

20. How can I make my navigationbar transparent instead of blurry?

  1. [self.navigationBar setBackgroundImage:[UIImage new ]
  2. forBarMetrics:UIBarMetricsDefault];
  3. self.navigationBar.shadowImage = [UIImage new ];
  4. self.navigationBar.translucent = YES;

21. How to change the color and position of UITextField placeholder?

Inherit UITextField and override this method:

  1. - ( void ) drawPlaceholderInRect:(CGRect)rect {
  2. [[UIColor blueColor] setFill];
  3. [self.placeholder drawInRect:rect withFont:self.font lineBreakMode:UILineBreakModeTailTruncation alignment:self.textAlignment];
  4. }

22. Why do you know so many strange tricks?

Go ask questions on Stack Overflow, boy!

<<:  Must read: Seven insights from 100 top APP developers

>>:  Cook's attack is Apple's retreat

Recommend

Worth collecting: 70 common mistakes in operations!

Introduction : Whether you are a newcomer or a ve...