小编jja*_*tie的帖子

关闭包含在 UIHostingController 中的 SwiftUI 视图

我已经将我的登录视图控制器重写为 SwiftUI View。该SignInView被包裹在一UIHostingController子类中(final class SignInViewController: UIHostingController<SignInView> {}),并且被呈现模态,全屏,当符号是必要英寸

一切正常,除了我不知道如何SignInViewControllerSignInView. 我试过添加:

@Environment(\.isPresented) var isPresented
Run Code Online (Sandbox Code Playgroud)

SignInView并将其分配给false当登录成功,但这并没有出现与UIKit的互操作到。我怎样才能关闭视图?

uikit ios swiftui

36
推荐指数
7
解决办法
1万
查看次数

UITableViewAutomaticDimension直到滚动才能工作

首次加载表视图时,所有可见单元格都是estimatedRowHeight.当我向下滚动时,单元格会自动调整大小,当我向上滚动时,最初估计的单元格正在自动调整大小.

一旦细胞自动调整大小,它们似乎永远不会再回到估计的高度.

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .Plain, target: nil, action: nil)
    self.tableView.estimatedRowHeight = 80.0
    self.tableView.rowHeight = UITableViewAutomaticDimension

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
Run Code Online (Sandbox Code Playgroud)

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cellIdentifier = "Cell"
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: …
Run Code Online (Sandbox Code Playgroud)

uitableview uikit ios swift

24
推荐指数
4
解决办法
2万
查看次数

合并将一个发布者变成另一个

我使用一个 OAuth 框架,它异步创建经过身份验证的请求,如下所示:

OAuthSession.current.makeAuthenticatedRequest(request: myURLRequest) { (result: Result<URLRequest, OAuthError>) in
            switch result {
            case .success(let request):
                URLSession.shared.dataTask(with: request) { (data, response, error) in
                    // ...
                }
             // ...
             }
        }
Run Code Online (Sandbox Code Playgroud)

我试图让我的 OAuth 框架使用Combine,所以我知道有一个发布者版本的makeAuthenticatedRequest方法,即:

public func makeAuthenticatedRequest(request: URLRequest) -> AnyPublisher<URLRequest, OAuthError>
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用它来替换上面的调用站点,如下所示:

OAuthSession.current.makeAuthenticatedRequestPublisher(request)
    .tryMap(URLSession.shared.dataTaskPublisher(for:))
    .tryMap { (data, _) in data } // Problem is here
    .decode(type: A.self, decoder: decoder)
Run Code Online (Sandbox Code Playgroud)

如上所述,问题在于将发布者的结果转换为新的发布者。我该怎么做呢?

ios swift combine

12
推荐指数
1
解决办法
5163
查看次数

核心数据模型似乎没有反映在应用程序中

我在Xcode 8 beta 1中遇到了Core Data的困难.旧的应用程序将编译并运行良好,但所有新应用程序编译并运行正常,直到尝试插入新的NSManagedObject.

最初我认为它与错误地删除旧的xcdatamodel并重新构建另一个有关,但在制作一个全新的应用程序并制作一个简单的实体"A"之后,我无法在运行时创建类A的对象.

我试过使用let a = A(context: myMOC)哪个返回错误:

类"MyApp.A"的NSManagedObject必须具有有效的NSEntityDescription.

尝试旧的let a = NSEntityDescription.insertNewObject(forEntityName: "A", into: context) as! A返回错误:

无法将"NSManagedObject_A_"(0x7fd114626f80)类型的值转换为"MyApp.A"(0x10d2fdf28).

我已经检查了我的xcdatamodel十几次,以确保我拼写正确的一切,并创建了一个新项目进行测试,以确保我没有在设置CD时出错.思考?

更新: xcdatamodel包内容是这样的:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="11147.23" systemVersion="16A201w" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
    <entity name="Coordinate" syncable="YES" codeGenerationType="class">
        <attribute name="latitude" optional="YES" attributeType="Double" defaultValueString="0.0" usesScalarValueType="YES" syncable="YES"/>
        <attribute name="longitude" optional="YES" attributeType="Double" defaultValueString="0.0" usesScalarValueType="YES" syncable="YES"/>
        <relationship name="route" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="Route" inverseName="coordinates" inverseEntity="Route" syncable="YES"/>
    </entity>
    <entity name="Route" syncable="YES" codeGenerationType="class">
        <attribute name="uuid" optional="YES" attributeType="String" syncable="YES"/>
        <relationship name="coordinates" …
Run Code Online (Sandbox Code Playgroud)

core-data ios swift xcode8

9
推荐指数
1
解决办法
3502
查看次数

SwiftUI NavigationView 在滚动时折叠

当我滚动时,如何让 NavigationBar 从折叠largeinline?现在ScrollView正在滚动其NavigationBar标题下方的内容,但在后退按钮之前。

    var body: some View {
        NavigationView {
            ScrollView {
                VStack {
                    ForEach(0...5) { _ in
                        ExerciseView()
                    }
                }
                .padding()
            }
        }
        .navigationBarTitle("Testing", displayMode: .automatic)
Run Code Online (Sandbox Code Playgroud)

ios swift swiftui

8
推荐指数
1
解决办法
7711
查看次数

在presetentation演示之前失去对transitioningDelegate的引用

我在Xcode beta 2和iOS 10 beta 2之前有一个工作的自定义UIPresentationController.我没有更改任何代码,但现在正在呈现标准模式演示文稿.

UIPresentationController的Apple示例代码中有一条说明:

对于将使用自定义演示控制器的演示文稿,该演示控制器也可以是transitioningDelegate.这避免了在源视图控制器中引入另一个对象或实现.

transitioningDelegate没有对其目标对象的强引用.为了防止在调用-presentViewController之前释放presentationController:animated:completion:NS_VALID_UNTIL_END_OF_SCOPE属性被附加到声明中.

我在演示之前和之后检查了呈现的视图控制器上的transitioningDelegate.在它是我的自定义UIPresentationController之前,但它之后是零.我的猜测是该引用正在发布,但我在Swift中找不到与NS_VALID_UNTIL_END_OF_SCOPE等效的内容.编辑:我已经验证过transitioningDelegate是在演示之前设置的,然后是时候呈现的nil.

我在呈现视图控制器中的代码:

@IBAction func buttonAction(_ sender: UIButton) {
    let secondViewController = storyboard!.instantiateViewController(withIdentifier: "NewViewController") as! NewViewController
    let presentationController = MyPresentationController(presentedViewController: secondViewController, presenting: self)
    presentationController.initialFrame = button.frame
    secondViewController.transitioningDelegate = presentationController

    // Move map
    let pixelsToMove: CGFloat = mapView.frame.height / 4
    let region = self.mapView.region

    self.mapView.setRegion(region, offsetBy: pixelsToMove, animated: true)

    // Delegate to NewViewController
    secondViewController.mapView = mapView
    mapView.delegate = secondViewController

    print(secondViewController.transitioningDelegate)
    UIView.animate(withDuration: 0.3, animations: {
        let tabBar = self.tabBarController!.tabBar
        tabBar.frame.origin.y += tabBar.frame.height …
Run Code Online (Sandbox Code Playgroud)

uiviewcontroller ios swift uipresentationcontroller xcode8-beta2

7
推荐指数
2
解决办法
1173
查看次数

自定义模式演示文稿中的UIBlurEffect

我目前正在使用自定义屏幕的三分之一呈现视图控制器UIPresentationController.在我看来UIPresentationController,我presentedViewController在几个视图中包含圆角和阴影(就像在Apple的Custom Transitions示例应用程序中).我最近添加了一个UIVisualEffectView带有a UIBlurEffectpresentedViewController层次结构,但它显示很奇怪.视图现在是半透明的,但不模糊.

我认为这是因为模糊效果正确应用,但因为它是一个模态演示,它不会看到背后的视图,因此无法模糊它.有什么想法吗?这是相关的代码 override func presentationTransitionWillBegin()

    // Wrap the presented view controller's view in an intermediate hierarchy
    // that applies a shadow and rounded corners to the top-left and top-right
    // edges.  The final effect is built using three intermediate views.
    //
    // presentationWrapperView              <- shadow
    //   |- presentationRoundedCornerView   <- rounded corners (masksToBounds)
    //        |- presentedViewControllerWrapperView
    //             |- presentedViewControllerView (presentedViewController.view)
    //
    // SEE ALSO: The note in AAPLCustomPresentationSecondViewController.m. …
Run Code Online (Sandbox Code Playgroud)

ios swift uimodalpresentationcustom uivisualeffectview

7
推荐指数
1
解决办法
466
查看次数

MeasurementFormatter显示英尺和英寸

我一直在玩,MeasurementFormatter试图显示英制10"6'10 ft 6 in不成功的长度.设置LengthFormatter为时会正确执行此操作,但它不能很好地适应用户的区域设置(即,以度量为单位测量长度的国家/地区除外,指的是高度).有没有办法在格式化程序中强制执行此行为?isForPersonHeightUsetrue

编辑问题是确定如何选择测量单位.我可以选择英尺或英寸,但希望显示小数英尺,如:6'3"而不是6.25英尺.

formatter ios swift measurementformatter

7
推荐指数
1
解决办法
1820
查看次数

为什么类型推断在Swift 3中的这个switch语句中不起作用?

更新这在Swift 3.1中得到修复

在迁移if-elseswitch语句时,我注意到类型推断不起作用.为什么我需要HKQuantityTypeIdentifier在每个类型中指定case何时quantityTypeIdentifier属于该类型?

func process(samples: [HKSample]?, quantityTypeIdentifier: HKQuantityTypeIdentifier) {
    DispatchQueue.main.async { [weak self] in            
        if let quantitySamples = samples as? [HKQuantitySample] {
            for sample in quantitySamples {
                switch quantityTypeIdentifier {
                case HKQuantityTypeIdentifier.distanceWalkingRunning:
                    // code

                case HKQuantityTypeIdentifier.activeEnergyBurned:
                    // code

                case HKQuantityTypeIdentifier.heartRate:
                    // code

                default:
                    fatalError("Quantity Type Identifier not implemented \(quantityTypeIdentifier)")
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我能够调用这样的函数:

process(samples: samples, quantityTypeIdentifier: .distanceWalkingRunning)
Run Code Online (Sandbox Code Playgroud)

swift

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

如何使用healthkit解决此错误?

我在我的ios Swift应用程序中添加了healthkit的代码,但是我收到了错误...

 /* Ask for permission to access the health store */
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)

if HKHealthStore.isHealthDataAvailable(){

  healthStore.requestAuthorizationToShareTypes(typesToShare,
    readTypes: typesToRead,
    completion: {(succeeded: Bool, error: NSError!) in

      if succeeded && error == nil{
        println("Successfully received authorization")
      } else {
        if let theError = error{
          println("Error occurred = \(theError)")
        }
      }

    })

} else {
  println("Health data is not available")
}

}
Run Code Online (Sandbox Code Playgroud)

发生错误=错误域= com.apple.healthkit代码= 4"缺少com.apple.developer.healthkit权利." UserInfo = 0x7fa748534b00 {NSLocalizedDescription =缺少com.apple.developer.healthkit权利.}

怎么解决这个?

谢谢!

swift xcode6 ios8 healthkit

4
推荐指数
4
解决办法
1万
查看次数

SwiftUI多行文本总是在1行处截断

我正在尝试创建选项列表供用户选择。调试预览显示了总体外观。我的问题是,通过nil.lineLimitMultipleChoiceOption不允许文本增长超过1行。我该如何纠正?

struct Card<Content: View> : View {
    private let content: () -> Content
    init(content: @escaping () -> Content) {
        self.content = content
    }

    private let shadowColor = Color(red: 69 / 255, green: 81 / 255, blue: 84 / 255, opacity: 0.1)

    var body: some View {

        ZStack {
            self.content()
                .padding()
                .background(RoundedRectangle(cornerRadius: 22, style: .continuous)
                    .foregroundColor(.white)
                    .shadow(color: shadowColor, radius: 10, x: 0, y: 5)
            )
            }
            .aspectRatio(0.544, contentMode: .fit)
            .padding()
    }
}

struct MultipleChoiceOption : …
Run Code Online (Sandbox Code Playgroud)

swiftui

2
推荐指数
2
解决办法
1213
查看次数

使用地图访问数组中的下一个元素

我想将点数组转换成线数组,如下所示:

let lines = points.map { $1 - $0 }
Run Code Online (Sandbox Code Playgroud)

我得到错误

上下文闭包类型'(Point)-> _'需要1个参数,但是在闭包主体中使用了2个

我知道为什么得到这个,但是我可能发誓我曾经在地图闭包中使用多个参数看到过SO上的示例代码。有没有我找不到的类似功能可以做到这一点?

swift map-function

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