1 设置页面为横屏
// 设置页面为横屏
UIDevice.currentDevice().setValue(UIInterfaceOrientation.LandscapeLeft.rawValue, forKey: "orientation")
// MARK:- 设置页面为横屏
override func shouldAutorotate() -> Bool {
return true
}
2 隐藏状态栏
// MARK:- 隐藏状态栏
override func prefersStatusBarHidden()->Bool {
return true
}
3 处理导航条显示消失
// MARK:- 处理导航条显示消失
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.navigationController!.setNavigationBarHidden(true, animated:false)
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController!.setNavigationBarHidden(false, animated: false)
}
4 setEdgesForExtendedLayout
在iOS 7中,苹果引入了一个新的属性,
叫做[UIViewController setEdgesForExtendedLayout:],
它的默认值为UIRectEdgeAll。
当你的容器是navigation controller时,默认的布局将从navigation bar的顶部开始。
这就是为什么所有的UI元素都往上漂移了44pt。
func setEdgesForExtendedLayout() {
if (self.respondsToSelector("setEdgesForExtendedLayout:")) {
self.edgesForExtendedLayout = UIRectEdge.None
}
}
5 产生不重复的随机数
import Foundation
/**
产生不重复的随机数
使用
var A = Random.number(5)
print(A) // [4, 2, 3, 5, 1]
A = Random.number(5)
print(A) // [4, 5, 2, 1, 3]
var Apro = Random.numberPro(10, end: 20)
print(Apro) // [20, 12, 17, 13, 16, 15, 14, 19, 11, 18]
Apro = Random.numberPro(10, end: 20)
print(Apro) // [20, 17, 16, 18, 15, 14, 13, 11, 19, 12]
*/
struct Random {
/**
* 如区间表示 ==> [1, end]
*/
static func number(end: Int) -> [Int] {
var startArr = Array(1...end)
var resultArr = Array(count: end, repeatedValue: 0)
for i in 0.. (start, end]
*/
static func numberPro(start: Int, end: Int) -> [Int] {
let scope = end - start
var startArr = Array(1...scope)
var resultArr = Array(count: scope, repeatedValue: 0)
for i in 0..
6 显示所有iPhone上的字体
import UIKit
class Helper {
/**
显示所有iPhone上的字体
*/
func showAllFonts() {
let familyNames = UIFont.familyNames()
for familyName in familyNames {
print("family:\(familyName)")
let fontNames = UIFont.fontNamesForFamilyName(familyName as String)
for fontName in fontNames {
print("font:\(fontName)")
}
print("-------------")
}
}
}
7 邮箱网址手机号码等正则判断
/**
邮箱网址手机号码等正则判断
使用
- Validate.email("[email protected]").isRight // false
- Validate.URL("https://www.baidu.com").isRight // true
- Validate.IP("192.168.0.107").isRight // true
*/
import Foundation
enum Validate {
case email(_: String)
case phoneNum(_: String)
case carNum(_: String)
case username(_: String)
case password(_: String)
case nickname(_: String)
case URL(_: String)
case IP(_: String)
var isRight: Bool {
var predicateStr:String!
var currObject:String!
switch self {
case let .email(str):
predicateStr = "^([a-z0-9_\\.-]+)@([\\da-z\\.-]+)\\.([a-z\\.]{2,6})$"
currObject = str
case let .phoneNum(str):
predicateStr = "^((13[0-9])|(15[^4,\\D]) |(17[0,0-9])|(18[0,0-9]))\\d{8}$"
currObject = str
case let .carNum(str):
predicateStr = "^[A-Za-z]{1}[A-Za-z_0-9]{5}$"
currObject = str
case let .username(str):
predicateStr = "^[A-Za-z0-9]{6,20}+$"
currObject = str
case let .password(str):
predicateStr = "^[a-zA-Z0-9]{6,20}+$"
currObject = str
case let .nickname(str):
predicateStr = "^[\\u4e00-\\u9fa5]{4,8}$"
currObject = str
case let .URL(str):
predicateStr = "^(https?:\\/\\/)?([\\da-z\\.-]+)\\.([a-z\\.]{2,6})([\\/\\w \\.-]*)*\\/?$"
currObject = str
case let .IP(str):
predicateStr = "^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"
currObject = str
}
let predicate = NSPredicate(format: "SELF MATCHES %@" ,predicateStr)
return predicate.evaluateWithObject(currObject)
}
}