Literals in Swift Code
可以让我们在代码中快速的选取color,image,file
@1 code
Image Literal
@2 code
Color Literal
@3
file 没有UI界面来选择文件
不过也可使用代码快捷选取
let citiesJSONURL = #fileLiteral(resourceName: "Cities.json")
Navigation bar
1.1 Navigation Prompt
iOS8之前
navigationItem.prompt = "Navigation prompts appear at the top."
必须放在
viewDidAppear
之后。这是个已知Bug
iOS8+
已更正
1.2 Extended navigation bar
navigation bar 在7以后本身的frame 是无法随意更改的。不过有解决办法
@1 首先将bar 特殊属性更改掉
//去掉半透明属性
navigationController!.navigationBar.isTranslucent = false
//去掉bar下面灰色细线
navigationController!.navigationBar.shadowImage = #imageLiteral(resourceName: "TransparentPixel")
//设置背景色
navigationController!.navigationBar.setBackgroundImage(#imageLiteral(resourceName: "Pixel"), for: .default)
@2 之后添加我们想要的view在下面,不过我们得在扩展的view下面模拟bar的灰色细线
利用 阴影
layer.shadowOffset = CGSize(width: 0, height: CGFloat(1) / UIScreen.main.scale)
layer.shadowRadius = 0
layer.shadowColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1).cgColor
layer.shadowOpacity = 0.25
1.3 其他
shadowImage 要与 backgroundImage一同调用
局部更改back backIndicatorImage和backIndicatorTransitionMaskImage一同调用
UIImage
1.图片的拉伸
open func resizableImage(withCapInsets capInsets: UIEdgeInsets) -> UIImage
capInsets 是规定了不能拉伸的范围
2.color -> image
UIGraphicsBeginImageContextWithOptions(CGSize(width: 1, height: 1), false, UIScreen.main.scale)
let context = UIGraphicsGetCurrentContext()
context?.setFillColor(UIColor(red: 1, green: 2, blue: 1, alpha: 0).cgColor)
UIRectFill(CGRect(x: 0, y: 0, width: 1, height: 1))
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()