小编Tom*_*hen的帖子

SwiftUI GeometryReader 不会在中心布局自定义子视图

我有一个自定义视图:

struct ImageContent: View {
    var body: some View {
        Image("smile")
            .resizable()
            .scaledToFit()
    }
}
Run Code Online (Sandbox Code Playgroud)

哪个被放置到另一个视图中GeometryReader

var body: some View {
    GeometryReader { geometry in
        ImageContent()
        //Image("smile").resizable().scaledToFit()
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是,ImageContent视图不是在屏幕上居中,而是被放置在顶部,但是,通过删除ImageContent子视图并将视图的内容直接添加到几何阅读器中将解决这个问题(见图)。

此外,删除GeometryReader也可以解决问题。

我需要子视图,因为我将实现一些额外的逻辑,还需要 ,GeometryReader因为有一个手势添加到Image使用它的 。

任何的想法?

截屏

swiftui geometryreader

13
推荐指数
4
解决办法
4342
查看次数

沉默"没有任何布局约束的视图可能会在具有preferredContentSize设置的特定视图控制器上剪切其内容"

我有一个固定大小的视图控制器(它不能调整大小).此特定视图控制器在窗口中显示为工作表,我通过设置禁用了调整大小preferredContentSize.由于它不能再调整大小,我没有任何约束地离开了视图.但是,当我构建应用程序时,我收到了很多警告:

Views without any layout constraints may clip their content or overlap other views.
Run Code Online (Sandbox Code Playgroud)

我知道如果你不在标准窗口中添加任何约束,它将在调整大小时剪切.但是,在我的特定窗口上禁用了调整大小,以便我知道它不会发生.有没有办法让这个警告沉默?这个特定的视图控制器有很多视图,它阻止了我在问题导航器中可能遇到的其他有用的警告.

该视图控制器通过"Sheet"segue呈现.

macos interface-builder nslayoutconstraint

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

如何从静态图像中读取QR码

我知道您可以使用AVFoundation设备的相机扫描QR码.现在问题来了,我怎样才能从静态UIImage对象中做到这一点?

qr-code ios

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

SpriteKit场景不适合iPhone X上的整个视图

iPhone X几天前发布.我正在尝试将我的一个游戏更新到新手机.但是,在具有SpriteKit场景的一个场景中,场景不会缩放以填充整个屏幕.

以下是显示场景的代码:

GameScene *scene = [GameScene unarchiveFromFile:@"GameScene"];
scene.scaleMode = SKSceneScaleModeFill;
// skView is basically self.view casted to SKView
[skView presentScene:scene];
Run Code Online (Sandbox Code Playgroud)

SKS文件中场景的大小为2048x1536.

这就是它的样子:

图片

红色部分是场景内容.一个有趣的事情是,背景(蓝色和灰色,确实正确显示).

有任何解决这个问题的方法吗?我只是拉伸场景来填充显示器(没有任何东西被圆角和凹口覆盖)

iphone ios sprite-kit

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

如何将双引号放入Swift String

我正在编写一些代码来处理Swift中带双引号的字符串.这是我到目前为止所做的:

func someMethod {
    let string = "String with \"Double Quotes\""
    dealWithString(string)
}

func dealWithString(input: String) {
    // I placed a breakpoint here.
}
Run Code Online (Sandbox Code Playgroud)

当我运行代码时,断点像往常一样停在那里但是当我将以下内容输入调试器时:

print input
Run Code Online (Sandbox Code Playgroud)

这就是我得到的:

(String) $R0 = "String with \"Double Quotes\""
Run Code Online (Sandbox Code Playgroud)

我用反斜杠得到了这个字符串.但是,如果我试图从源中删除反斜杠,它将给我编译错误.这有解决方法吗?

string swift

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

未使用 macOS 通知服务扩展

我的 macOS 应用程序有一个通知服务扩展。

这是该扩展的代码:

import UserNotifications

class NotificationService: UNNotificationServiceExtension {

    var contentHandler: ((UNNotificationContent) -> Void)?
    var bestAttemptContent: UNMutableNotificationContent?

    override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
        print("Extension received notification!")

        self.contentHandler = contentHandler
        bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)

        bestAttemptContent?.title = "Title modified!"
    }

    override func serviceExtensionTimeWillExpire() {
        // Called just before the extension will be terminated by the system.
        // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push …
Run Code Online (Sandbox Code Playgroud)

macos notifications unnotificationserviceextension

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

如何强制iOS应用以人像模式启动

我制作了一个在第一个视图控制器(启动屏幕后立即显示的控制器)上同时支持横向和纵向模式的应用程序。我可以以纵向模式启动此应用,将其旋转至横向,然后再回到纵向,没有任何问题。但是,只要该应用最初以横向模式启动,它的行为就会非常奇怪。我想到的一个简单的解决方法是强制应用以纵向模式启动,然后让用户能够在启动后将其旋转为横向。

有什么办法可以做到这一点?

我使用的是Xcode 8.3,iOS 10.3,正在测试我的应用的设备是iPad Pro 12.9。

orientation ipad ios

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

使用fileNamed初始化SKAudioNode返回nil

这是我第一次使用SKAudioNode.首先,我在我GameScene班级的顶部宣布了一个属性:

var backgroundMusic: SKAudioNode!
Run Code Online (Sandbox Code Playgroud)

现在我添加了一个帮助方法:

func playBackgroundMusic(name: String) {
    if backgroundMusic != nil {
        backgroundMusic.removeFromParent()
    }

    backgroundMusic = SKAudioNode(fileNamed: name)
    backgroundMusic.autoplayLooped = true
    addChild(backgroundMusic)
}
Run Code Online (Sandbox Code Playgroud)

现在我这样调用这个方法:

playBackgroundMusic("ABC.caf")
Run Code Online (Sandbox Code Playgroud)

它在这一行引发致命错误:

backgroundMusic.autoplayLooped = true
Run Code Online (Sandbox Code Playgroud)

说:在解开可选值时意外发现nil.

我确实做了以下事情

  • ABC.caf在我的项目中,并列在复制包资源中.
  • 拼写正确.

现在还应该在哪里检查错误?

编辑:

这是我的配置信息:

  • Xcode 7.3
  • 带iOS 9.3.2的iPhone
  • iOS 9.3模拟器

设备和模拟器都不起作用.

EDIT2:

我将我的代码更改为以下内容:

func playBackgroundMusic(name: String) {
    if backgroundMusic != nil {
        backgroundMusic.removeFromParent()
    }

    let temp = SKAudioNode(fileNamed: name)
    temp.autoplayLooped = true
    backgroundMusic = temp
    //backgroundMusic = SKAudioNode(fileNamed: "SpaceGame.caf")
    //backgroundMusic.autoplayLooped = true
    addChild(backgroundMusic)
} …
Run Code Online (Sandbox Code Playgroud)

ios sprite-kit swift skaudionode

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

NSTask/bin/echo:/ bin/echo:无法执行二进制文件

我正在制作一个需要运行shell脚本的OS X App.这是我的快速代码:

func runTask(arguments: [String]) {
    output.string = ""

    task = NSTask()
    task.launchPath = "/bin/bash"
    task.arguments = arguments;

    errorPipe = NSPipe()
    outputPipe = NSPipe()

    task.standardError = errorPipe
    task.standardOutput = outputPipe

    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(didCompleteReadingFileHandle(_:)), name: NSFileHandleReadCompletionNotification, object: task.standardOutput!.fileHandleForReading)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(didCompleteReadingFileHandle(_:)), name: NSFileHandleReadCompletionNotification, object: task.standardError!.fileHandleForReading)

    errorPipe.fileHandleForReading.readInBackgroundAndNotify()
    outputPipe.fileHandleForReading.readInBackgroundAndNotify()

    task.launch()
}

func didCompleteReadingFileHandle(sender: NSNotification) {
    let data: NSData = sender.userInfo![NSFileHandleNotificationDataItem] as! NSData;
    let string = NSString(data: data, encoding: NSUTF8StringEncoding)!
    // The output property is a NSTextView object
    output.string?.appendContentsOf(String(string))
}
Run Code Online (Sandbox Code Playgroud)

现在我尝试调用该runTask方法: …

macos nstask swift

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

如何通过给定边界来修剪 NSImage

我有一个 NSImage 对象,我有一个 CIDetector 对象,用于检测该图像上的 QR 代码。检测到后,我想修剪该图像,使其中只包含二维码。这就是我获得二维码边界的方法:

NSArray *features = [myQRDetector featureInImage:myCIImage];
CIQRCodeFeature *qrFeature = features[0];
CGRect qrBounds = qrFeature.bounds;
Run Code Online (Sandbox Code Playgroud)

现在我如何修剪图像,使其仅包含qrBounds变量描述的区域。

macos nsimage

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

如何在C中枚举

我有一个看起来像这样的方法:

static inline float average(int numbers, ...) {

}
Run Code Online (Sandbox Code Playgroud)

你如何使用for循环来获得我已经将数字放入的所有值.例如:

average(1, 3, 5, 7);
Run Code Online (Sandbox Code Playgroud)

c for-loop

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