首页 > iOS > 正文

SearchBar

iOS9 以后默认不显示取消按钮, 通过UISearchBarDelegate将其出现

 // MARK: UISearchBarDelegate
    
 func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
     searchBar.becomeFirstResponder()
     searchBar.setShowsCancelButton(true, animated: true)
 }
 
 func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
     searchBar.resignFirstResponder()
     searchBar.setShowsCancelButton(false, animated: true)
 }
 
 func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
     
     // 点击搜索
     
     print("搜索 -> \(searchBar.text)")
     
     searchBar.resignFirstResponder()
     searchBar.setShowsCancelButton(false, animated: true)
 }
 
 func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
     searchBar.resignFirstResponder()
     searchBar.setShowsCancelButton(false, animated: true)
 }

修改取消按钮

  • 第一种 for item in searchBar.subviews[0].subviews { if item.isKind(of: UIButton.self){ let button:UIButton = item as! UIButton button.setTitle("取消", for: .normal) } }
  • 第二种 (推荐) UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.classForCoder() as! UIAppearanceContainer.Type]).title = "取消" UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.classForCoder() as! UIAppearanceContainer.Type]).setTitleTextAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 15), NSForegroundColorAttributeName: UIColor.white], for: .normal)

dispatch_async

Swift3 修改为

DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { // in half a second...
            print("Are we there yet?")
        }

stackoverflow了解更多

Privacy Settings in iOS 10

iOS10Xcode8中对权限的访问更加”变态”了, 应用需要那些权限都要在info.plist文件中添加.

如果需要添加获取相机的权限, 则需要添加

<key>NSCameraUsageDescription</key>
	<string>Used to scan barcodes</string>

才会显示

其他权限

相机权限描述:

<key>NSCameraUsageDescription</key>
<string>cameraDesciption</string>

通信录:

<key>NSContactsUsageDescription</key>
<string>contactsDesciption</string>

麦克风:

<key>NSMicrophoneUsageDescription</key>
<string>microphoneDesciption</string>

相机:

<key>NSPhotoLibraryUsageDescription</key>
<string>photoLibraryDesciption</string>

参考:

iOS10 权限崩溃问题

Privacy Settings in iOS 10

模拟器问题

在模拟器上运行时出现下面错误

subsystem: com.apple.siri, category: Intents, enable_level: 1, persist_level: 1, default_ttl: 0, info_ttl: 0, debug_ttl: 0, generate_symptoms: 0, enable_oversize: 0, privacy_setting: 0, enable_private_data: 0

解决方法

  1. Go in Product -> Scheme -> Edit Scheme
  2. Run Section on the left, select Argument Tab and in Environment Variable put this.

swift3.0 – 懒加载

 lazy var imageArtwork:UIImage = {
        
       var image = try! UIImage(data: Data(contentsOf: URL(string: (RadioSingleton.sharedInstance.radioInfo?.coverImg)!)!))
    
        return image!
    }()

持续更新

猜你喜欢
发表评论

电子邮件地址不会被公开。 必填项已用*标注

评论信息
picture loss