踩過的坑和奇怪的API筆記
- UINavigationController 導航欄
- 設定導航欄顏色
- 去掉導航欄下面的分割線
- 設定導航欄字體和字體顏色
- UITabBar
- UITabBar和導航欄結合使用
- 獲取UITabBar的高度
- 跳轉界面隱藏UITabBar的方案
- UITableView
- UITableViewCell里點擊事件不生效
- 設定分割線內邊距
- 隱藏所有分割線分割線
- 隱藏底部多余行及分割線
- UICollectionView & UIScrollView
- cell里的按鈕區域只能點擊無法滑動
- 允許多選
- ScrollView是否顯示滑動條
- ScrollView使ContentSize小于UIScrollView大小也能滑動
- ScrollView分頁滑動
- UIScrollView截長圖
- UIView
- 截圖
- 拼接圖片
- 獲取當前UIView的UIVIewController
- UIImage
- 重設圖片大小和等比例縮放
- 裁切圖片
- 獲取純色UIImage
- 改變顏色
- 獲取網路圖片
- UIButton
- UIButton中圖片和文字的位置變換
- UIColor & Color
- UIColor 十六進制
- Color 十六進制
- CGRect
- CGRect等比例縮放
- 點擊事件不生效可能的原因
- Date
- 獲取農歷日期
- 當前月有多少天
- 今天是今年第幾天
- 本周是今年第幾周
- String
- sha256
- unicode
- urlEncoded
- Base64
- Dictionary
- 合并字典
- Application
- 手機真實寬高(px)
- 設計寬高
- 手機序列號(唯一標識)
- 手機別名:用戶定義的名稱
- 設備名稱
- 手機系統版本
- 手機型號
- 地方型號 (國際化區域名稱)
- 是否是IPad
UINavigationController 導航欄
設定導航欄顏色
navigationController?.navigationBar.barTintColor = .blue
去掉導航欄下面的分割線
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.shadowImage = UIImage()
- 此時導航欄顏色和View背景相同,應該類似吧導航欄設定透明了的效果
- 使用方法一設定導航欄顏色為
.clear并不能達到相同效果 - 本方法會導致方法一設定導航欄顏色失效,此時使用以下代碼設定導航欄顏色
navigationController?.navigationBar.backgroundColor = .orange
設定導航欄字體和字體顏色
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.black,NSAttributedString.Key.font : UIFont.systemFont(ofSize: 25)]
UITabBar
UITabBar和導航欄結合使用
tabBarCtrl = UITabBarController()
tabBarCtrl.tabBar.tintColor = .DesignGreen
let view1 = ViewHome()
view1.tabBarItem.image = UIImage(named: "xiaozujian")
view1.tabBarItem.title = "組件"
let view2 = ViewIcon()
view2.tabBarItem.image = UIImage(named: "tubiao")
view2.tabBarItem.title = "圖示"
let view1 = UINavigationController.init(rootViewController: viewHome)
let view2 = UINavigationController.init(rootViewController: viewIcon)
tabBarCtrl.addChild(view1)
tabBarCtrl.addChild(view2)
tabBarCtrl.selectedIndex = 0 //默認選擇
self.window?.rootViewController = tabBarCtrl
window?.makeKeyAndVisible()
獲取UITabBar的高度
let height = tabBarCtrl.tabBar.bounds.size.height
跳轉界面隱藏UITabBar的方案
override func viewWillDisappear(_ animated: Bool) {
tabBarCtrl.tabBar.isHidden = true
}
UITableView
UITableViewCell里點擊事件不生效
使用 cell.contentView.addSubview 代替 cell.addSubview
設定分割線內邊距
tableView.separatorInset = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
隱藏所有分割線分割線
- 方法一:
self.separatorStyle = .none
- 方法二:
tableView.separatorInset = UIEdgeInsets(top: 0, left: tableView.frame.width, bottom: 0, right: 0)
隱藏底部多余行及分割線
tableview.tableFooterView = UIView(frame: CGRect.zero)
UICollectionView & UIScrollView
cell里的按鈕區域只能點擊無法滑動
使用`UILabel`或`UIImageView`等系結`UITapGestureRecognizer` 代替`UIButton`
func InitUI(){
let img = UIImageView(frame:CGRect(xxxxxx))
let singleTapGesture = UITapGestureRecognizer(target: self, action: #selector(ClickBtn(_:)))
img.addGestureRecognizer(singleTapGesture)
img.isUserInteractionEnabled = true
}
@objc func ClickBtn(_ tap:UITapGestureRecognizer){
}
允許多選
collection.allowsMultipleSelection = true
ScrollView是否顯示滑動條
scrollView.showsHorizontalScrollIndicator = false
scrollView.showsVerticalScrollIndicator = false
ScrollView使ContentSize小于UIScrollView大小也能滑動
scrollView.alwaysBounceVertical = true
scrollView.alwaysBounceHorizontal = true
ScrollView分頁滑動
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
if (!decelerate)
{
scrollView.setContentOffset(CGPoint(x: CGFloat(Int(scrollView.contentOffset.x/pageWidth + 0.5))*pageWidth, y: scrollView.contentOffset.y), animated: true)
selectPage = Int(scrollView.contentOffset.x/pageWidth + 0.5)
}
}
func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {
scrollView.setContentOffset(CGPoint(x: CGFloat(Int(scrollView.contentOffset.x/pageWidth + 0.5))*pageWidth, y: scrollView.contentOffset.y), animated: true)
selectPage = Int(scrollView.contentOffset.x/pageWidth + 0.5)
}
UIScrollView截長圖
///截長圖
extension UIScrollView {
/// 截長屏Image
var captureLongImage: UIImage? {
var image: UIImage? = nil
let savedContentOffset:CGPoint? = contentOffset
let savedFrame:CGRect? = frame
contentOffset = .zero
frame = CGRect(x: 0, y: 0,
width: contentSize.width,
height: contentSize.height)
UIGraphicsBeginImageContextWithOptions(
CGSize(width: frame.size.width,
height: frame.size.height),
false,
0.0)
layer.render(in: UIGraphicsGetCurrentContext()!)
image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
contentOffset = savedContentOffset ?? contentOffset
frame = savedFrame ?? frame
return image
}
fileprivate func writeImageToAlbum(_ image: UIImage) {
UIImageWriteToSavedPhotosAlbum(image, self, #selector(image(image:didFinishSavingWithError:contextInfo:)), nil)
}
@objc fileprivate func image(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo:UnsafeRawPointer) {
if let _ = error as NSError? {
print("保存失敗,請重試")
} else {
print("保存成功!")
}
}
}
UIView
截圖
extension UIView {
/// 截屏Image
func captureImage(size:CGSize)-> UIImage? {
// 引數①:截屏區域 引數②:是否透明 引數③:清晰度
UIGraphicsBeginImageContextWithOptions(frame.size, false, 0.0)
layer.render(in: UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
func captureImageInPos( rect: CGRect) -> UIImage? {
UIGraphicsBeginImageContext(rect.size)
guard let context = UIGraphicsGetCurrentContext() else { return nil }
context.translateBy(x: -rect.minX, y: -rect.minY)
self.draw(.zero)
let croppedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return croppedImage
}
}
拼接圖片
//拼接圖片
func combinTwoImage(image1:UIImage,image2:UIImage) -> UIImage
{
let width = max(image1.size.width, image2.size.width)
let height = image1.size.height + image2.size.height
let offScreenSize = CGSize.init(width: width, height: height)
UIGraphicsBeginImageContextWithOptions(offScreenSize, false, 0.0)
let rect = CGRect.init(x:0, y:0, width:width, height:image1.size.height)
image1.draw(in: rect)
let rect2 = CGRect.init(x:0, y:image1.size.height, width:width, height:image2.size.height)
image2.draw(in: rect2)
let image:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext();
return image;
}
獲取當前UIView的UIVIewController
extension UIView{
func ParentCtrl()->UIViewController{
var vc:UIResponder = self
while vc.isKind(of: UIViewController.self) != true {
vc = vc.next!
}
return vc as! UIViewController
}
}
UIImage
重設圖片大小和等比例縮放
extension UIImage {
/**
* 重設圖片大小
*/
func reSizeImage(reSize: CGSize )-> UIImage {
//UIGraphicsBeginImageContext(reSize);
UIGraphicsBeginImageContextWithOptions (reSize, false , UIScreen .main.scale);
//繪制圖片
self.draw(in: CGRect(x: 0,
y: 0,
width: reSize.width,
height: reSize.height))
let reSizeImage: UIImage = UIGraphicsGetImageFromCurrentImageContext ()!
UIGraphicsEndImageContext ();
return reSizeImage;
}
/**
* 等比率縮放
*/
func scaleImage(scaleSize: CGFloat )-> UIImage {
let reSize = CGSize(width: self .size.width * scaleSize, height: self .size.height * scaleSize)
return reSizeImage(reSize: reSize)
}
}
裁切圖片
extension UIImage{
func crop(_ rect: CGRect) -> UIImage? {
let img = self.reSizeImage(reSize: UIScreen.main.currentMode!.size)
var newRect = rect
let WIDTH:CGFloat = UIScreen.main.bounds.width
let RELWIDTH = UIScreen.main.currentMode?.size.width
let scale = (RELWIDTH!/WIDTH)
newRect.origin.x *= scale*scale
newRect.origin.y *= scale*scale
newRect.size.width *= scale*scale
newRect.size.height *= scale*scale
guard let imageRef = img.cgImage?.cropping(to: newRect) else { return nil }
return UIImage(cgImage: imageRef)
}
}
獲取純色UIImage
extension UIImage {
convenience init?(color: UIColor, size: CGSize = CGSize(width: 1, height: 1)) {
let rect = CGRect(origin: .zero, size: size)
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0)
color.setFill()
UIRectFill(rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
guard let cgImage = image?.cgImage else { return nil }
self.init(cgImage: cgImage)
}
}
改變顏色
extension UIImage {
func changeColor(color:UIColor) -> UIImage {
UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
let context = UIGraphicsGetCurrentContext()
context?.translateBy(x: 0, y: self.size.height)
context?.scaleBy(x: 1.0, y: -1.0)//kCGBlendModeNormal
context?.setBlendMode(.normal)
let rect = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height)
context?.clip(to: rect, mask: self.cgImage!);
color.setFill()
context?.fill(rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}
}
獲取網路圖片
func GetImgByUrl(path:String)->UIImage{
let url = NSURL(string: path)!
let data = NSData(contentsOf: url as URL)!
return UIImage(data: data as Data,scale: 1.0)!
}
UIButton
UIButton中圖片和文字的位置變換
enum ButtonEdgeInsetsStyle {
case ButtonEdgeInsetsStyleTop // image在上,label在下
case ButtonEdgeInsetsStyleLeft // image在左,label在右
case ButtonEdgeInsetsStyleBottom // image在下,label在上
case ButtonEdgeInsetsStyleRight // image在右,label在左
}
extension UIButton {
func layoutButtonEdgeInsets(style:ButtonEdgeInsetsStyle,space:CGFloat) {
let labelWidth : CGFloat = self.titleLabel?.frame.size.height ?? 36
let labelHeight : CGFloat = self.titleLabel?.frame.size.width ?? 20
var imageEdgeInset = UIEdgeInsets.zero
var labelEdgeInset = UIEdgeInsets.zero
let imageWith = self.imageView?.frame.size.width
let imageHeight = self.imageView?.frame.size.height
switch style {
case .ButtonEdgeInsetsStyleTop:
imageEdgeInset = UIEdgeInsets(top: -labelHeight-space/2.0, left: (bounds.width - imageWith!)/2, bottom: 0, right: 0)
labelEdgeInset = UIEdgeInsets(top: 0, left: -imageWith!, bottom: -imageHeight!-space/2.0, right: 0)
case .ButtonEdgeInsetsStyleLeft:
imageEdgeInset = UIEdgeInsets(top: 0, left: -space/2.0, bottom: 0, right: space/2.0);
labelEdgeInset = UIEdgeInsets(top: 0, left: space/2.0, bottom: 0, right: -space/2.0);
case .ButtonEdgeInsetsStyleBottom:
imageEdgeInset = UIEdgeInsets(top: 0, left: 0, bottom: -labelHeight-space/2.0, right: -labelWidth)
labelEdgeInset = UIEdgeInsets(top: -imageHeight!-space/2.0, left: -imageWith!, bottom: 0, right: 0)
case .ButtonEdgeInsetsStyleRight:
// To Do print("坐標是====\(labelWidth)=====\(space)")
imageEdgeInset = UIEdgeInsets(top: 0, left: labelWidth+space/2.0, bottom: 0, right: -labelWidth-space/2.0)
labelEdgeInset = UIEdgeInsets(top: 0, left: -imageWith!-space/2.0, bottom: 0, right: imageWith!+space/2.0)
}
self.titleEdgeInsets = labelEdgeInset
self.imageEdgeInsets = imageEdgeInset
}
}
UIColor & Color
UIColor 十六進制
extension UIColor{
convenience init(hex: Int, alpha: CGFloat = 1){
let components = (
R: CGFloat((hex >> 16) & 0xff) / 255,
G: CGFloat((hex >> 08) & 0xff) / 255,
B: CGFloat((hex >> 00) & 0xff) / 255
)
self.init(
red: components.R,
green: components.G,
blue: components.B,
alpha: alpha
)
}
}
Color 十六進制
extension Color {
init(hex: Int, alpha: Double = 1) {
let components = (
R: Double((hex >> 16) & 0xff) / 255,
G: Double((hex >> 08) & 0xff) / 255,
B: Double((hex >> 00) & 0xff) / 255
)
self.init(
.sRGB,
red: components.R,
green: components.G,
blue: components.B,
opacity: alpha
)
}
}
CGRect
CGRect等比例縮放
extension CGRect{
init(x:CGFloat,y:CGFloat,w:CGFloat,h:CGFloat,scale:CGFloat){
self.init(x: x*scale, y: y*scale, width: w*scale, height: h*scale)
}
}
點擊事件不生效可能的原因
- 部分控制元件默認用戶互動式關閉的,需要手動打開,UIView或父級View的isUserInteractionEnabled為false
view.isUserInteractionEnabled = true - 當前點擊的UIView或區域超出了父級View的范圍,
- 當前點擊的UIView或區域被其他透明View覆寫
- UITableViewCell里點擊事件不生效,使用
cell.contentView.addSubview代替cell.addSubview
Date
獲取農歷日期
extension Date{
static var ChineseDate: String{
//設定農歷日歷
let chinese = Calendar(identifier: .chinese)
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "zh_CN")
formatter.calendar = chinese
//日期樣式
formatter.dateStyle = .long
//公歷轉為農歷
let str = formatter.string(from: Date())
return String(str.dropFirst(4))
}
}
當前月有多少天
extension Date{
static var DayCountOfMonth:Int{
let ca = Calendar.init(identifier: .gregorian)
var lastMonthComps = DateComponents()
lastMonthComps.month = 0
let curDate = ca.date(byAdding: lastMonthComps, to: Date())
var interval:Double = 0
var firstDate:Date = Date()
var lastDate :Date = Date()
let calendar = NSCalendar.current
let bl = calendar.dateInterval(of: .month, start: &firstDate, interval: &interval, for: curDate!)
if bl {
lastDate = firstDate.addingTimeInterval(interval-1)
}
let day = Calendar.current.component(.day, from: lastDate);
return day
}
}
今天是今年第幾天
extension Date{
static var dayOfYear:Int{
let from = Calendar.current.dateComponents([.year], from: Date())
return (Calendar.current.dateComponents([.day], from: Calendar.current.date(from: from)!,to: Date()).day ?? 0) + 1
}
}
本周是今年第幾周
extension Date{
static var weekOfYear:Int{
return Calendar.current.component(.weekOfYear, from: Date())
}
}
String
sha256
extension String {
func sha256() -> String{
if let stringData = self.data(using: String.Encoding.utf8) {
return hexStringFromData(input: digest(input: stringData as NSData))
}
return ""
}
private func hexStringFromData(input: NSData) -> String {
var bytes = [UInt8](repeating: 0, count: input.length)
input.getBytes(&bytes, length: input.length)
var hexString = ""
for byte in bytes {
hexString += String(format:"%02x", UInt8(byte))
}
return hexString
}
private func digest(input : NSData) -> NSData {
let digestLength = Int(CC_SHA256_DIGEST_LENGTH)
var hash = [UInt8](repeating: 0, count: digestLength)
CC_SHA256(input.bytes, UInt32(input.length), &hash)
return NSData(bytes: hash, length: digestLength)
}
}
unicode
extension String {
var unicodeStr:String {
let tempStr1 = self.replacingOccurrences(of: "\\u", with: "\\U")
let tempStr2 = tempStr1.replacingOccurrences(of: "\"", with: "\\\"")
let tempStr3 = "\"".appending(tempStr2).appending("\"")
let tempData = tempStr3.data(using: String.Encoding.utf8)
var returnStr:String = ""
do {
returnStr = try PropertyListSerialization.propertyList(from: tempData!, options: [.mutableContainers], format: nil) as! String
} catch {
print(error)
}
return returnStr.replacingOccurrences(of: "\\r\\n", with: "\n")
}
var unicode:String{
var str = ""
for scalar in self.unicodeScalars {
str = str + "\\u\(String(scalar.value, radix: 16))"
}
return str
}
}
urlEncoded
extension String {
//將原始的url編碼為合法的url
func urlEncoded() -> String {
let encodeUrlString = self.addingPercentEncoding(withAllowedCharacters:
.urlQueryAllowed)
return encodeUrlString ?? ""
}
//將編碼后的url轉換回原始的url
func urlDecoded() -> String {
return self.removingPercentEncoding ?? ""
}
}
Base64
extension String {
//Base64編碼
func encodBase64() -> String?{
let strData = self.data(using: String.Encoding.utf8)
let base64String = strData?.base64EncodedString(options: NSData.Base64EncodingOptions.init(rawValue: 0))
return base64String
}
//Base64解碼
func decodeBase64() -> String?{
let decodedData = NSData(base64Encoded: self, options: NSData.Base64DecodingOptions.init(rawValue: 0))
let decodedString = NSString(data: decodedData! as Data, encoding: String.Encoding.utf8.rawValue) as String?
return decodedString
}
}
Dictionary
合并字典
extension Dictionary {
public func deepMerged(with other: [Key: Value]) -> [Key: Value] {
var result: [Key: Value] = self
for (key, value) in other {
if let value = value as? [Key: Value], let existing = result[key] as? [Key: Value] {
result[key] = (existing.deepMerged(with: value) as! Value)
} else {
result[key] = value
}
}
return result
}
}
Application
手機真實寬高(px)
let RELWIDTH = UIScreen.main.currentMode?.size.width
let RELHEIGHT = UIScreen.main.currentMode?.size.height
設計寬高
let WIDTH:CGFloat = UIScreen.main.bounds.width let HEIGHT:CGFloat = UIScreen.main.bounds.height
手機序列號(唯一標識)
UIDevice.current.identifierForVendor?.uuidString
手機別名:用戶定義的名稱
UIDevice.current.name
設備名稱
UIDevice.current.systemName
手機系統版本
UIDevice.current.systemVersion
手機型號
UIDevice.current.model
地方型號 (國際化區域名稱)
UIDevice.current.localizedModel
是否是IPad
UIDevice.current.userInterfaceIdiom == .pad
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/295210.html
標籤:其他
