2016年5月31日 星期二

讓tableCell的分隔線從頭連到尾(iOS 8以後)

//viewDidLoad
tableView.layoutMargins = UIEdgeInsetsZero
tableView.separatorInset = UIEdgeInsetsZero

//setup Cell
cell!.layoutMargins = UIEdgeInsetsZero

2016年5月25日 星期三

UIScrollView裡面的點擊事件反應很慢

var delaysContentTouchesBool

If the value of this property is true, the scroll view delays handling the touch-down gesture until it can determine if scrolling is the intent. If the value is false , the scroll view immediately callstouchesShouldBegin:withEvent:inContentView:. The default value is true.

同樣一個UISegmentedControl,加在ScrollView裡面反應就異常的慢,原來是UIScrollView的subview上的touch事件會被延遲,目的是為了確定當次的手勢不是要滑動。
如果你想讓你的scrollView以點擊事件為主:
yourScrollView.delaysContentTouches = false
如果你想讓你的scrollView以滑動事件為主:
yourScrollView.delaysContentTouches = true

2016年5月20日 星期五

讓UIButton的文字title顯示在圖片上

// 因為UIButton預計是將title接在image的右邊,所以要用UIEdgeInsets把title往左移到跟圖片相等的位置。
if let imageWidth = yourButton.imageView?.intrinsicContentSize().width {
        yourButton.titleEdgeInsets = UIEdgeInsetsMake(0, -imageWidth, 0, 0)

        }

// 其實setImage的作用應該是在Button上的加上小圖示
[[小圖示]按鈕文字]

// 另外一種作法是用setBackgroundImage來作為Button的底圖
button.setBackgroundImage(UIImage(named: "home_button.png"), forState: .Normal)

button.setBackgroundImage(UIImage(named: "home_button_light.png"), forState: .Highlighted)

2016年4月28日 星期四

為你的iOS專案加入新字型


  1. 把字型檔拉進你的project中(只能用.ttf或.otf的檔案)
  2. 去info.plist加入Fonts provided by application,把新增的檔名加入item0的string(若有下一個檔案就加到item1的string)。
  3. print("\(UIFont.familyNames())")看看你新增的字型有沒有被加進去。
  4. 最後去print("\(UIFont.fontNamesForFamilyName("Noto Sans CJK TC"))")看看你加入的family裡面有那些字型你可以用。self.titleLabel.font = UIFont(name: "NotoSansCJKtc-Bold", size: 20)

p.s. 可以用appearance把這些font都變成預設值。
struct Theme {
    static func setDefaultFont() {
        UILabel.appearance().font = UIFont(name: "NotoSansCJKtc-Regular", size: 17)
        UITextField.appearance().font = UIFont(name: "NotoSansCJKtc-Regular", size: 17)
        UITextView.appearance().font = UIFont(name: "NotoSansCJKtc-Regular", size: 17)
    }
}

2016年4月26日 星期二

自動調整ScrollView讓textField適應鍵盤高度

直接把我的code貼上來,重點是不要改變scrollView.contentSize,因為auto layout時會自動設定。
----------------------------------------------------------------
//
//  BaseScrollViewController.swift
//  BaseSettings
//
//  Created by HarveyHu on 4/26/16.
//  Copyright © 2016 HarveyHu. All rights reserved.
//

import UIKit
import SnapKit
import RxSwift

class BaseScrollViewController: UIViewController {
    let contentView = UIView()
    let scrollView = UIScrollView()

    var originalContentInsets = UIEdgeInsetsZero
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
    }
    override func viewDidAppear(animated: Bool) {
        super.viewWillAppear(animated)
        originalContentInsets = scrollView.contentInset
    }
    
    override func setUI() {
        super.setUI()
        self.scrollView.addSubview(contentView)
        self.view.addSubview(scrollView)
        
    }
    
    override func setUIConstraints() {
        super.setUIConstraints()
        
        scrollView.snp_makeConstraints { (make) in
            make.top.equalTo(self.view)
            make.bottom.equalTo(self.view)
            make.leading.equalTo(self.view)
            make.trailing.equalTo(self.view)
        }


        contentView.snp_updateConstraints { (make) in
            make.top.equalTo(self.scrollView)
            make.leading.equalTo(self.scrollView)
            make.trailing.equalTo(self.scrollView)
            make.bottom.equalTo(self.scrollView)

        }
    }
    
    // MARK: - Keyboard
    @objc override func keyboardWasShown(aNotification: NSNotification)
    {
        guard let info = aNotification.userInfo, kbSize = info[UIKeyboardFrameBeginUserInfoKey]?.CGRectValue().size else {
            return
        }
        
        let contentInsets = UIEdgeInsetsMake(originalContentInsets.top, originalContentInsets.left, kbSize.height, originalContentInsets.right)
        self.scrollView.contentInset = contentInsets
        self.scrollView.scrollIndicatorInsets = contentInsets
    }
    
    @objc override func keyboardWillBeHidden(aNotification: NSNotification)
    {
        let contentInsets = originalContentInsets
        self.scrollView.contentInset = contentInsets
        self.scrollView.scrollIndicatorInsets = contentInsets
    }

}
------------------------------------------------------------------
要用的時候就繼承這個VC,把所有內容放到contentView上,但在contentView的constraints設定上要注意寛度可以固定,但高度不要寫死,要跟著內容高度而定。
contentView.snp_updateConstraints { (make) in
            make.width.equalTo(self.view)
            make.bottom.equalTo(self.theMostBottomView).offset(10)

        }

2016年4月21日 星期四

切換xcode預設路徑到不同位置

OSX有提供一個方便的指令
xcode-select -p 印出目前設定的路徑
xcode-select -s /yourXcodePath/Developer 設定路徑
xcode-select -r 重設回預設的路徑(/Applications/Xcode.app/Contents/Developer)