小编smn*_*mnk的帖子

将过滤器按钮添加到UISearchController

我想一个"过滤器"或"排序"按钮旁边的添加searchBarUISearchController.我已经尝试添加UIButtonsearchbar进入a UIView并将其设置为my UITableView tableView.tableHeaderView这不起作用,因为tableView.tableHeaderViewget的设置回到了UISearchBar解除控制器的时候.以下是我希望它的外观示例:在此输入图像描述

uitableview uisearchbar ios swift uisearchcontroller

10
推荐指数
3
解决办法
6076
查看次数

列出可访问的AirPrint打印机

我想显示一个列表,其中包含设备可通过AirPrint访问的所有打印机.

我开始使用它了UIPrinterPickerController.

无论如何以自定义的方式显示这个PickerController,让我们说将数据输入UITableView

另请注意,我打算在仅支持iPad的iPad上使用此应用程序 UIPrinterPickerController.presentFromRect(CGRect)

这就是它现在的样子.而不是这个弹出窗口应该有一个UITableView

打印机弹出窗口

ipad ios uiprintinfo uiprintinteractioncntrler swift

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

从另一个类访问IBOutlet

我正在从我的ViewController类中调用类函数,如下所示:

Buttons.set_cornerRadius(10)
Run Code Online (Sandbox Code Playgroud)

我有另一个.swift文件,我声明了这个函数:

class Buttons {

      class func set_cornerRadius(radius: CGFloat) {
          ViewController().someButton.layer.cornerRadius = radius
      }
}
Run Code Online (Sandbox Code Playgroud)

当我试图运行它时会抛出错误:"Unexpectedly found nil while unwrapping an optional Value".

我已经检查了Storyboard-IBOutlet连接.一切都是正确的.
如果我像这样在同一个类中调用该方法,一切正常:

class ViewController: UIViewController {

    @IBOutlet weak var someButton: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()
        set_cornerRadius(10)
    }

    func set_cornerRadius(radius: CGFloat) {
        someButton.layer.cornerRadius = radius
    }
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能解决这个问题?我做错了什么/不理解对吗?

提前致谢.

null class iboutlet ios swift

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

自动布局中的相对中心

我正在使用自动布局来定位我的视图。在我的情况下,这是一个UILabelUIStackView它上面和一个下面。该UIStackView配置为.fill所有它的arrangedSubviews有一个正确的heightConstraint。我想要做的是添加约束,以便UIStackViewmyLabel.bottomAnchor&之间的空间中垂直居中mySuperview.bottomAnchor

到目前为止我尝试过的是:

  • 添加顶部和底部约束UIStackView
    • _stackView.topAnchor.constraint(greaterThanOrEqualTo: _myLabel.bottomAnchor)
    • _stackView.bottomAnchor.constraint(greaterThanOrEqualTo: _mySuperview.bottomAnchor)

现在这有效,因为它只是将正确大小的UIStackView放在_mySuperview.

然而,我想要的是它在中心。我看到有一种方法叫做anchorWithOffset(to:),听起来正是我需要的,但找不到正确的使用方法。

所以我的问题是,如何将 a 居中放置UIView在两个给定的之间NSLayoutAnchors

编辑:我知道我可以UIView在我的其他两个视图之间添加一个约束并将我的UIStackView集中在那里。然而,我正在寻找更纯粹的解决方案。

ios autolayout swift

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

在 Swift UITest 中检测从右到左的语言

我正在尝试设置我的 UITests 以进行本地化。特别是对于阿拉伯语和任何其他从右到左 (RTL)语言。

就我而言,我有一个swipe(direction: SwipeDirection)值为SwipeDirection( .up, .down, .left, .right) 的函数。

如果我现在想删除一个单元格,我只需这样做,cell.swipe(.left)UITest就会删除该单元格。由于 UI 在RTL 语言上翻转,因此必须更改为cell.swipe(.right).

我找不到任何检测当前设备语言的方法,也找不到UILayoutDirection

我已经尝试像这样检查它:

let isLeftToRight = UIView.userInterfaceLayoutDirection(for: UIView().semanticContentAttribute) == .leftToRight
Run Code Online (Sandbox Code Playgroud)

并且

let isLeftToRight = UIApplication.shared.userInterfaceLayoutDirection == .leftToRight
Run Code Online (Sandbox Code Playgroud)

true但无论语言如何,他们俩总是会回来。UILayoutDirection还有其他方法可以检查我不知道的电流吗?

谢谢你!

xcode automated-tests ios swift xcode-ui-testing

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

为自动UITesting设置模拟器

我需要设置我的Simulators for Automated UITesting.特别是从键盘上删除Autocorrection,Spellchecking,Prediction等.

我正在为一个模拟器这样做:

plutil -replace KeyboardAutocapitalization -bool NO -- ~/Library/Developer/CoreSimulator/Devices/319FF855-F54C-4BB7-BDED-F0CC4B1FF8FC/data/Library/Preferences/com.apple.Preferences.plist
Run Code Online (Sandbox Code Playgroud)

问题是,当测试在另一台设备上运行时,它将不会设置模拟器,因为UUID是硬编码的.

我也能够像这样启动/选择模拟器:

currentUUID="$(xcrun simctl list devices | grep "iPhone 7 (" | egrep -o -i "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}")"
Run Code Online (Sandbox Code Playgroud)

我必须放在哪里iPhone 7 (才能获得UUIDiPhone 7而不是7+.这也只有在我之前在Xcode中选择iPhone 7作为模拟器时才有效.另一种方法是更换iPhone 7 (with booted.

但为了使其工作,脚本需要模拟器已经运行.当它已经运行时,更改plist文件实际上不会更新模拟器上的设置.

我怎样才能启动模拟器,然后在设置之前抓住它的UUID?

谢谢.

automated-tests simulator ios run-script xcode-ui-testing

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

如何异步drawInRect

我正在尝试加载一个巨大的图像(谈论131.072x131.072像素)很好地平铺成一堆URL的256x256像素的512x512块.

一旦我的函数返回Image我想在一个正确位置的Rect中绘制它.

由于这个过程需要一段时间,我想以异步方式运行整个过程.以下是我到目前为止所尝试的内容:

override func drawRect(rect: CGRect) {
    let firstColumn = Int(CGRectGetMinX(rect) / sideLength)
    let lastColumn = Int(CGRectGetMaxX(rect) / sideLength)
    let firstRow = Int(CGRectGetMinY(rect) / sideLength)
    let lastRow = Int(CGRectGetMaxY(rect) / sideLength)

    let qos = Int(QOS_CLASS_USER_INITIATED.rawValue)
    dispatch_async(dispatch_get_global_queue(qos, 0)) { () -> Void in

        for row in firstRow...lastRow {
            for column in firstColumn...lastColumn {

                let url = NSURL(string: "https://someURL/\(row)/\(column).jpg")
                let tile = UIImage(data: NSData(contentsOfURL: url!)!)!

                let x = self.sideLength * CGFloat(column)
                let y = self.sideLength * CGFloat(row)
                let point = CGPoint(x: …
Run Code Online (Sandbox Code Playgroud)

asynchronous grand-central-dispatch ios swift

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