Skip to content

iOS 开发小结 #3

@aTreey

Description

@aTreey

Swift4.1 中UIImagebase64转化

  • 使用 public init?(base64Encoded base64String: String, options: Data.Base64DecodingOptions = default)函数

官方解释
Initialize a Data from a Base-64 encoded String using the given options.
Returns nil when the input is not recognized as valid Base-64.
- parameter base64String: The string to parse.
- parameter options: Encoding options. Default value is [].

默认是 options 是 [] 项目中为设置 options 时返回的data 值为nil,无法生成图片

  • 需要设置 options 为 .ignoreUnknownCharacters 即可,作用是忽略未知的非基64字节,每行结束字符
let imageData = Data(base64Encoded: base64String, options: .ignoreUnknownCharacters)
 if let data = imageData {
    let image = UIImage(data: data)
    imageView.image = image
  }

Swift4.1 中获取类名及通过类名生成对象

获取类名

 let clsName = String(describing: controller.classForCoder)
 let dyName = type(of: controller) 
 print("clsName=\(clsName)") 
 print("dyName = \(dyName)")
 print(controller.description) 
 print(controller.classForCoder.description())
 print(String(controller.description))
print(type(of:controller))
print(String(describing: type(of:controller))) print(NSStringFromClass(type(of:controller)).components(separatedBy: ".").last!)

结果

tableView cell 上添加ScrollView后tableViewcell不响应点击事件处理

scrollView.userInteractionEnabled = false;
[self.contentView addGestureRecognizer:scrollView.panGestureRecognizer];

事件传递、响应者链条、hitTest和pointInside的使用

  • hitTest: 返回的是最适合处理事件的View
  • pointInside:判断事件发生的位置是否处于当前视图范围内
    只有pointInside返回Yes的处理事件的view才能真正响应点击事件

使用UISearchBar 底部有时会出现1像素的黑线解决办法

if (@available(iOS 11.0, *)) {
         [[self.searchBar.heightAnchor constraintEqualToConstant:44.0] setActive:YES];
     }
self.searchDisplayController.searchBar.barStyle = UISearchBarStyleProminent

swift 和 OC 混编 Podfile 中使用use_frameworks! 相关问题

使用 use_frameworks!关键词后 头文件导入时直接使用 @import XXX; 这样更加方便效率也更高

项目中颜色图片统一管理

  • swift 项目中给UIColor 添加分类
  • xcode 9 中的 Color Set 方式设置(iOS 11 以上才可以使用)
  • 使用第三方库
    R.swift
 myImageView.image = #imageLiteral(resourceName: "icon_cat")
 myImageView.image = R.image.icon_cat()

sizeToFit 和 sizeThatFits 区别

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions