您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 程式設計技術

iOS 開發常用小技巧彙編

編輯:iOS 程式設計技術

以下技巧都來自實際項目中遇到的問題。解決方法往往不止一種,這裡所列未必是最優解,歡迎交流指正。

  1. 防止同時點擊兩個按鈕進入兩個 view:

    [button setExclusiveTouch:YES];
  2. 6p 模擬器輸出寬度是 414,真機卻是 375。這是顯示模式設置問題,進「設置 → 顯示與亮度」把顯示模式改為「標準」即可。

  3. 圖片拉伸:

    UIImage* img = [UIImage imageNamed:@"2.png"];
    UIEdgeInsets edge = UIEdgeInsetsMake(0, 10, 0, 10);
    //UIImageResizingModeStretch:拉伸指定矩形區域填充圖片
    //UIImageResizingModeTile:平鋪指定矩形區域填充圖片
    img = [img resizableImageWithCapInsets:edge resizingMode:UIImageResizingModeStretch];
    self.imageView.image = img;
  4. 解決 UITableView 頂部空白、cell 不在最頂端的問題。iOS 7 起 viewController 新增了 automaticallyAdjustsScrollViewInsets 屬性,設為 NO 讓它不自動調整即可:

    self.automaticallyAdjustsScrollViewInsets = NO;
  5. 修改 tableViewCell 選中狀態的背景色:

    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
    cell.selectedBackgroundView.backgroundColor = [UIColor whiteColor];
  6. 預設選中第一個 cell:

    NSInteger selectedIndex = 0;
    NSIndexPath *selectedIndexPath = [NSIndexPath indexPathForRow:selectedIndex inSection:0];
    [_leftSiftTable selectRowAtIndexPath:selectedIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
  7. 設置按鈕文字左對齊:

    timeBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    timeBtn.contentEdgeInsets = UIEdgeInsetsMake(0,10, 0, 0);
  8. 修改 textField 的 placeholder 字體顏色與大小:

    textField.placeholder = @"username is in here!";
    [textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
    [textField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];
  9. 讓 UILabel 字體自適應寬度。方法一:lab.adjustsFontSizeToFitWidth = YES;;方法二:

    label.numberOfLines = 0;
    label.lineBreakMode = NSLineBreakByWordWrapping;
  10. 修改狀態欄字體顏色(僅黑、白兩色,預設黑色)。設為白色:(1) 在 plist 添加 Status bar style,值為 UIStatusBarStyleLightContent;(2) 在 Info.plist 設置 UIViewControllerBasedStatusBarAppearance 為 NO。

  11. 自定義 leftBarButtonItem 後若想恢復系統右劃返回:

    self.navigationController.interactivePopGestureRecognizer.delegate = self;
  12. 去掉導航欄下方的黑線:

    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
  13. 點擊 cell 後取消其選中狀態:

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
  14. 修改 pageControl 顏色:

    _pageControl.currentPageIndicatorTintColor = SFQRedColor;
    _pageControl.pageIndicatorTintColor = SFQGrayColor;
  15. userInteractionEnabled 屬性:UIView 預設 YES,UIImageView 預設 NO。為 YES 表示該視圖可交互、不響應父視圖;為 NO 則不可交互,其上的子視圖也不會響應。

  16. 去掉 UITableView section 的懸停粘性,使其隨滾動消失或出現:

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
        if (scrollView == _tableView) {
            CGFloat sectionHeaderHeight = 36;
            if (scrollView.contentOffset.y <= sectionHeaderHeight && scrollView.contentOffset.y >= 0) {
                scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
            } else if (scrollView.contentOffset.y >= sectionHeaderHeight) {
                scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
            }
        }
    }
  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved