The pits encountered in iOS development, continuous updates


The pits encountered in iOS development, continuous updates

Button layout, top and bottom typesetting

In use, there are plenty of opportunities to use a button in the form of context typesetting. In this case, most of them modify the buttons directly.imageEdgeInsetsandtitleEdgeInsets。eg:

 Btn.titleEdgeInsets = UIEdgeInsetsMake(Btn.imageView.frame.size.height ,-Btn.imageView.frame.size.width, 0.0,0.0);
 Btn.imageEdgeInsets = UIEdgeInsetsMake(0.0, 0.0,0.0, -Btn.titleLabel.bounds.size.width);

Similar to this, the layout of buttons can be changed into a context layout. Of course, useimageEdgeInsetsandtitleEdgeInsetsYou can also modify the spacing between pictures and text on the button, which is less compact and better in performance.


  1. Delete redundant partitioning lines
    In development, there are times when the number of data sources does not cover the entire screen.tableViewA large section of the cell is left blank below. At this point, all you need to do is set uptableViewOffooterView。eg:

     xxTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero]

    Or it’s in the process of implementation.UITableViewDelegateA proxy method.

    -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section

    The return value, set to a very small value, is invalid if set to 0. So in use, I prefer the direct first method.

  2. The top of the dividing line
    Always feelUITableViewThe default dividing line will automatically leave a certain distance, which is estimated to be 10,000 damage to Virgo. After checking the information on the internet, it is the default top if you say before iOS6. (throw. In the age of iOS 6, I was forced to brush a generation of magic machine defy. (escape. Later, in some data, it was only necessary to reset it.layoutMarginsperhapsseparatorInset。 BecauselayoutMarginsIt is only after iOS 8, so in the process of implementation, we need to make a judgment on the system version. The former is used in the version after iOS 8, and the latter is used in the version after iOS 7. Eg:

    [cell setLayoutMargins:UIEdgeInsetsZero];         // iOS8+
    [cell setSeparatorInset:UIEdgeInsetsZero];        // iOS7

    I amUITableViewDataSourceImplemented in a proxy method.

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath


modifyUITabBarWhen the background color is changed, it can’t be changed.backgroundColorProperty to modify, so that the result of the modification will not have any effect. There are two ways to modify:

  1. By modifyingtintColorAttributes, howeverUITabBarDefault has a transparent property——translucent。 This value defaults toYES。 If it is not set toNOIf so, it shows.tabBarThere will be a mask or something like that. Let’s just go straight to the code.

    self.tabBar.barTintColor = [UIColor redColor];
    self.tabBar.translucent = NO;
  2. Will oneviewInsert intotabBarFirst, that is——

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, _tabBar.bounds.size.height)];
    view.backgroundColor = [UIColor redColor];
    [_tabBar insertSubview:view atIndex:0];

Modify the selectedtabBarItemBackground, then it needs to be revisedselectionIndicatorImage。 However, when used before, it was found that the image could not be stretched after clicking. In eachitemLeft and right sides of the will leave a small section of uncovered parts.

 _tabBar.selectionIndicatorImage = [UIImage imageNamed:@"xx"];

Set upselectedItemOfselectedImageWhen you need to modify the imagerenderingModeOtherwise, it will be the default blue. Namely –

UIImage *selectedImage = [UIImage imageNamed:@"xx"];
selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
_tabBar.selectionIndicatorImage = selectedImage;

Set upselectedItemWhen the font color is selected, you can modify it.initializeChange within the method, that is——

+(void)initialize {
    UITabBarItem *item = [UITabBarItem appearanceWhenContainedIn:self, nil];
    NSMutableDictionary *colorDic = [NSMutableDictionary dictionary];
    colorDic[NSForegroundColorAttributeName] = [UIColor redColor];
    [item setTitleTextAttributes:colorDic forState:UIControlStateSelected];

It can also be modified directly.UITabBarItemOfappearanceThat is, –

[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName ] forState:UIControlStateSelected];

Such a form. It can also be used to modify fonts and so on.

iOS7+Return gesture with right stroke

iOS7After that, a gesture event returned by the right stroke is added. But the premise is to useiOSNativeNavigationBar。 If it is customizedNavigationBarIn that case, the function of returning the right row to the previous page will fail. At this point, just use the following two sentences of code, you can continue to use this function.

self.navigationController.interactivePopGestureRecognizer.enabled = YES;
self.navigationController.interactivePopGestureRecognizer.delegate = self;

Get what it belongs toUIViewController

Sometimes, in oneUIViewUnder the class, to do some operations, such as a custom method, you need to pass in aUIViewControllerThe value of the value, then how to pass the value at this time. You can use the following code to find which one belongs toUIViewController

-(UIViewController*)viewController {
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder* nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController*)nextResponder;
    return nil;


Sometimes, in useUIScrollViewIn the process, you will find that all the controls in it have been moved down by 20. At this point, in the correspondingcontrollerIn addition

self.automaticallyAdjustsScrollViewInsets = NO