小编Ric*_*hiy的帖子

UIStackView:将子视图推到边缘(“无限”间距)

UIStackView用来定义列表的布局。

我正在尝试实现与此类似的效果:

||Item1---spacing---Item2||
Run Code Online (Sandbox Code Playgroud)

因此,这两个项目被推到UIStackView.

但是,项目组合在一起的每一个可能的组合distributionalignment

||Item1-Item2-----spacing-----||
Run Code Online (Sandbox Code Playgroud)

我尝试设置一个大的间距值,即 500。在这种情况下,Item2只是离开屏幕。

有什么办法来“配合” Item1,并Item2到的左,右两侧UIStackView,同时允许间隔取决于屏幕宽度的变化?

cocoa-touch uikit ios autolayout uistackview

5
推荐指数
1
解决办法
1671
查看次数

AutoSizing单元格:单元格宽度等于CollectionView

我正在使用Autolayout和UICollectionView的AutoSizing单元格.

我可以在单元初始化的代码中指定约束:

  func configureCell() {
    snp.makeConstraints { (make) in
      make.width.equalToSuperview()
    }
  }
Run Code Online (Sandbox Code Playgroud)

然而,应用程序崩溃,因为单元格尚未添加到collectionView.

问题

  1. cell生命周期的哪个阶段,可以用cell's width?添加约束?

  2. 有没有作出任何默认方式cellwidthequal to the of the的CollectionView without accessing an instance of UIScreen orUIWindow`?

编辑 问题不重复,因为它不是关于如何使用AutoSizing单元格功能,而是在单元生命周期的哪个阶段应用约束以使用AutoLayout 实现所需的结果.

autosize ios autolayout uicollectionview swift

5
推荐指数
1
解决办法
5778
查看次数

Swift中的drop(where:{})remove(where:{})函数?

Swift标准库中是否有作用于集合的函数,采用谓词并返回从该集合中删除的值?

目前,我必须分两步实施:

guard let idx = allAnnotations.index(where: {$0 is MKUserLocation}) else {return}
let userLocation = allAnnotations.remove(at: idx) as! MKUserLocation
Run Code Online (Sandbox Code Playgroud)

但是我想,存在类似的功能。

目标

我有以下数组:

[Type1, Type1, Type1, Type1, Type1, Type1, Type2]
Run Code Online (Sandbox Code Playgroud)

Type2数组中可能存在也可能不存在。除了这两种以外,没有其他类型。

我需要将其分为两个元素:

[Type1, Type1, Type1, Type1, Type1, Type1]
Run Code Online (Sandbox Code Playgroud)

Type2?
Run Code Online (Sandbox Code Playgroud)

那就是我要寻找的功能。

arrays collections swift

5
推荐指数
1
解决办法
817
查看次数

Swift:Codable - 提取单个编码密钥

我有以下代码来提取编码密钥中包含的JSON:

let value = try! decoder.decode([String:Applmusic].self, from: $0["applmusic"])
Run Code Online (Sandbox Code Playgroud)

这成功处理了以下JSON:

{
  "applmusic":{
    "code":"AAPL",
    "quality":"good",
    "line":"She told me don't worry",
}
Run Code Online (Sandbox Code Playgroud)

但是,无法使用以下编码密钥提取JSON applmusic:

{
  "applmusic":{
    "code":"AAPL",
    "quality":"good",
    "line":"She told me don't worry",
  },
  "spotify":{
    "differentcode":"SPOT",
    "music_quality":"good",
    "spotify_specific_code":"absent in apple"
  },
  "amazon":{
    "amzncode":"SPOT",
    "music_quality":"good",
    "stanley":"absent in apple"
  }
}
Run Code Online (Sandbox Code Playgroud)

数据模型applmusic,spotify并且amazon是不同的.但是,我只需要提取applmusic和省略其他编码密钥.

我的Swift数据模型如下:

public struct Applmusic: Codable {
    public let code: String
    public let quality: String
    public let line: String
}
Run Code Online (Sandbox Code Playgroud)

API以完整的JSON响应,我不能要求它只给我所需的字段.

如何只解码json的特定部分?看起来,这Decodable …

json ios swift codable decodable

5
推荐指数
1
解决办法
3717
查看次数

Google Maps:通用链接格式-目标坐标-“不支持的链接Google Maps无法打开此链接”

什么是实现以下目的的正确URL格式:

  1. 使用Universal Link从iOS上的其他应用程序打开Goog​​le Maps应用程序。
  2. 根据两个坐标(纬度经度)设置目的地,并让用户选择运输方式。

什么不起作用:

let encoded = "https://www.google.com/maps/dir/?api=1&destination=-20.021999%2C57.579075"
let url = URL(string: encoded)!
UIApplication.shared.open(url, options: [:], completionHandler: nil)
Run Code Online (Sandbox Code Playgroud)

另外,我尝试使用addingPercentEncoding方法对URL进行编码:

let string = "https://www.google.com/maps/dir/?api=1&destination=\(lat),\(long)"
let encoded = string.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
let url = URL(string: encoded)!
UIApplication.shared.open(url, options: [:], completionHandler: nil)
Run Code Online (Sandbox Code Playgroud)

在两种情况下,结果都是相同的:“不支持的链接-Google Maps无法打开此链接” 在此处输入图片说明

另一方面,如果我删除Google Maps应用程序,或在模拟器中尝试相同的代码,则一切工作正常,并且可以完全达到我想要的结果:
在此处输入图片说明

为什么同一链接可以成功启动“ Web”应用程序,但是无法被本机应用程序识别?

有问题的字串:
https://www.google.com/maps/dir/?api=1&destination=-20.021999,57.579075

我要遵循的指南:Google Maps Guide

google-maps urlencode deep-linking swift ios-universal-links

5
推荐指数
1
解决办法
1296
查看次数

具有120 Hz的iOS设备-UIKit刷新率是多少

在大多数教程或建议中,将60 fps的值列为该应用程序的最终刷新率。即,如果该应用程序能够始终如一地以60 fps的速度提供用户体验,则没有更多的改进空间。

但是,对于具有120 Hz刷新率的较新设备(例如iPad Pro),情况如何?他们是否真的每秒重绘UI组件120次,这意味着成功准备帧的时间从大约16毫秒减少到8毫秒?

performance rendering refresh uikit ios

5
推荐指数
1
解决办法
78
查看次数

Swift:遵守带有“where”子句的通用方法的协议

概括:

我想创建一个其中Class<T>包含相应ClassDelegate协议的协议func<T>

目标:

通过多个对象类重用单个对象和行为。接收已有专门类的委托回调,无需将对象转换为特定类即可使用它。

示例代码:

具有通用方法的协议:

protocol GenericTableControllerDelegate: AnyObject {
    func controller<T>(controller: GenericTableController<T>, didSelect value: T)
}
Run Code Online (Sandbox Code Playgroud)

通用基子类UITableViewController

open class GenericTableController<DataType>: UITableViewController {
    weak var delegate: GenericTableControllerDelegate?
    var data = [DataType]()

    open override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let item = data[indexPath.row]
        delegate?.controller(controller: self, didSelect: item)
    }
}
Run Code Online (Sandbox Code Playgroud)

的专门版本GenericTableController

final class SpecializedTableController: GenericTableController<NSObject> {}
Run Code Online (Sandbox Code Playgroud)

-的客户端SpecializedTableController实现了结果,但需要类型转换才能访问专用数据类型:

final class ClientOfTableController: UIViewController, GenericTableControllerDelegate {
    // Works OK
    func …
Run Code Online (Sandbox Code Playgroud)

generics protocols ios swift swift-protocols

5
推荐指数
1
解决办法
1314
查看次数

iOS:以编程方式从应用程序退出到主屏幕并优雅地退出动画?

根据技术问答 QA1561,不建议任何方式退出应用程序。适当的方式是允许用户自行退出应用程序,而不是强制退出或中止应用程序的执行。

但是,Microsoft Teams iOS 应用程序以某种方式设法不仅避免了崩溃,而且以动画方式优雅地退出,类似于按下设备上的“主页”按钮。

MS Teams iOS 应用程序中的视频录制或程序退出

请注意,在上述示例中没有按下主页按钮。该应用程序以编程方式执行动画退出

  1. 这是 iOS 的记录功能吗?还是 MS Teams 的开发人员使用某种私有 API?
  2. 在任何情况下,如何以动画方式以编程方式退出应用程序,使其不会显示为崩溃?

如何在您的设备上重现此行为:

  1. 下载 MS Teams 应用程序
  2. 登入
  3. 将设置中的主题从“暗”更改为“亮”,反之亦然,然后单击“重新启动”。观察应用程序如何通过动画退出。

iphone homescreen iphone-privateapi ios home-button

5
推荐指数
1
解决办法
2610
查看次数

Swift Package Manager:两个相似的目标,仅依赖项不同

我正在将作为 Xcode 项目构建和测试的库迁移到 Swift Package Manager。该项目包含 3 个目标:库本身、具有不同依赖关系的相同库以及测试(应用程序)目标。

到目前为止,移植库很容易。

现在我有兴趣构建具有不同依赖项(模拟)的相同库,可以对其进行测试。

实际上,Package.swift 文件如下所示:

// swift-tools-version:5.4
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "TargetLibrary",
    platforms: [
        .iOS(.v14), .macOS(.v10_15)
    ],
    products: [
        .library(
            name: "TargetLibrary",
            targets: ["TargetLibrary"]
        ),
    ],
    dependencies: [
        .package(
            url: "ssh://git@github.com/DependentLib",
            .upToNextMinor(from: "1.3.18")
        ),
    ],
    targets: [
        .target(
            name: "TargetLibrary",
            dependencies: [
                // Using the "main" dependency for the "TargetLibrary"
                .product(name: "DependentLib", package: "DependentLib"),
            ],
            path: …
Run Code Online (Sandbox Code Playgroud)

dependencies ios swift swift-package-manager swift-package

5
推荐指数
0
解决办法
1436
查看次数

@_exported 导入但在 Objective-C 中

Swift 有一个有趣的关键字,可以在 中声明ModuleA

@_exported import Foundation
Run Code Online (Sandbox Code Playgroud)

然后,当在其他模块(例如ModuleB)中时,我导入ModuleA

import ModuleA

let currentDate = Date() // using a Foundation struct
Run Code Online (Sandbox Code Playgroud)

Foundation...即使我没有使用语句导入它,我也可以使用类型import。它会被自动导入,因为它已@_exportedModuleA.

现在我想在 Objective-C 中有类似的行为。

鉴于:

  • 3 个目标:TargetA(Swift)、ProtocolTarget(带有关键字注释类型的 Swift @objc) 和TargetB(ObjC)
  • 我想ProtocolTarget隐式导入,以便在TargetB导入时TargetA,来自 的所有方法ProtocolTarget都应该在TargetA.

我怎样才能实现这个目标?

import objective-c swift swift-package-manager swift-package

5
推荐指数
0
解决办法
624
查看次数