簡(jiǎn)單粗暴 不定期更新(始于161128)......
- 打開(kāi)關(guān)閉閃光燈
let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
if device.hasTorch {
do {
try device.lockForConfiguration()
} catch _ {
}
device.torchMode = AVCaptureTorchMode.On // .Off
device.unlockForConfiguration()
}
- UISearchBar 去除背景灰色
let searchBar = UISearchBar.init(frame: CGRect(x: 10, y: 5, width: ScreenWidth-70, height: 30))
for subview in searchBar.subviews {
if subview.isKind(of: NSClassFromString("UIView")!) && subview.subviews.count > 0 {
subview.subviews.first!.removeFromSuperview()
}
}
- 某一頁(yè)隱藏 navigationbar辽狈,下一頁(yè)顯示
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.setNavigationBarHidden(true, animated: true)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
navigationController?.setNavigationBarHidden(false, animated: true)
}
- delegate
protocol NewHomeTopSearchViewDelegate: NSObjectProtocol {
func topViewBeginSearch()
}
class NewHomeTopSearchView: UIView, UISearchBarDelegate {
var delegate: NewHomeTopSearchViewDelegate?
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
if let delegate = delegate {
delegate.topViewBeginSearch()
}
}
}
myPointLabel.snp.makeConstraints { (make)
make.top.equalTo(signButton.snp.top).offset(20)
make.height.equalTo(signButton)
make.left.equalToSuperview()
make.right.equalTo(signButton.snp.left).offset(-10)
}
- button 整體位置
button.contentHorizontalAlignment = UIControlContentHorizontalAlignment.left
button.contentVerticalAlignment = UIControlContentVerticalAlignment.top
- 類方法
class func newHomeGetItemHeight(ratioWidth: CGFloat, ratioHegiht: CGFloat, width: CGFloat) -> CGFloat {
return width * ratioHegiht / ratioWidth
}
- 碰到了一個(gè)約束的問(wèn)題:依據(jù) 屏幕w 動(dòng)態(tài)適應(yīng)了 itemH刮萌,但是由于 item 中 僅僅按照比例設(shè)置了 imgView的寬高,又由于 按比例計(jì)算了itemH娘扩,那么在不同的尺寸下(SE)着茸,imgView 與 item 動(dòng)態(tài)的改變了 H壮锻,但是其他的控件的高度沒(méi)變,所以其他控件就不正常的顯示了 ---> 解決:如果需要按照比例設(shè)置item涮阔,那么應(yīng)該給 item中的 每個(gè)控件 都添加比例約束
xib中 單個(gè)控件 按比例設(shè)置寬高約束:xib中固定好相對(duì)位置(比如 設(shè)置好上左右約束后 手動(dòng)設(shè)置正確的高度猜绣,然是不要把高度加入約束中,而是圖8-0) -> 按比例設(shè)置好寬高敬特,但不要添加到約束中 - 圖8-0
圖8-0
圖8-1
圖8-2
- appdelegate 獲取 storyboard 創(chuàng)建的 UITabBarController
let tabbarVC = self.window?.rootViewController as! UITabBarController
tabbarVC.tabBar.tintColor = UIColor.init(rgb: 0xFF2832)
- 修改狀態(tài)欄的背景色
extension UIApplication {
var statusBarView: UIView? {
return value(forKey: "statusBar") as? UIView
}
}
UIApplication.shared.statusBarView?.backgroundColor = .red
- 當(dāng) vc 在 nvc 中的時(shí)候掰邢,需要更改狀態(tài)欄顏色
// 狀態(tài)欄字體為白色,狀態(tài)欄和導(dǎo)航欄背景為黑色
self.navigationController?.navigationBar.barStyle = .black
// 狀態(tài)欄字體為黑色伟阔,狀態(tài)欄和導(dǎo)航欄背景為白色
self.navigationController?.navigationBar.barStyle = .default
- 獲取文字的寬高
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let text = name_label.text! as NSString
let length = text.boundingRect(with: CGSize.init(width: CGFloat.greatestFiniteMagnitude, height: 15), options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName : name_label.font], context: nil).size.width + 1
name_label.snp.remakeConstraints { (make) in
make.top.equalTo(41)
make.width.equalTo(length)
make.height.equalTo(15)
make.left.equalTo(avatarImgView.snp.right).offset(25)
}
}
- 獲取空間最大y
signature_label.frame.maxY
- 初始化一個(gè)存放 label 的數(shù)組
private var label_array = Array<UILabel>()
- 獲取當(dāng)前日期為星期幾
var calendar = NSCalendar.current
calendar.timeZone = TimeZone.init(identifier: "Asia/Shanghai")!
let components = calendar.component(.weekday, from: Date.init())
print(components)
- 判斷點(diǎn)擊區(qū)域
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = (touches as NSSet).anyObject() as! UITouch
let point = touch.location(in: self.view)
if !self.collectionView.frame.contains(point) {
self.navigationController?.popViewController(animated: true)
}
}
- 某一個(gè)vc 隱藏 statusbar
override var prefersStatusBarHidden: Bool {
return true
}
- 轉(zhuǎn) str 并保留兩位小數(shù)
String.init(format: "%.2f", doublevalue)
- UIKeyboardWillChangeFrame
NotificationCenter.default.addObserver(self, selector: #selector(keybordChanged), name: .UIKeyboardWillChangeFrame, object: nil)
- 圖片拉伸
private lazy var bgImgView: UIImageView = {
var image = UIImage.init(named: "r_beijing")
image = image?.stretchableImage(withLeftCapWidth: Int((image?.size.width)!/2.0), topCapHeight: Int((image?.size.height)!/2.0))
let imgView = UIImageView.init()
imgView.image = image
imgView.isUserInteractionEnabled = true
return imgView
} ()
不定期更新 不合適的地方 還請(qǐng)指點(diǎn)~ 感激不盡