小编kcs*_*cks的帖子

以编程方式将子视图添加到UIStackView

我正在尝试实现一个表格视图单元格,它显示一系列代表家居设施的图标(例如毛巾,wifi,洗衣等).单元格可能会显示大量设施(取决于房屋的数量),因此我将显示最多三个,并指示如果有三个以上,您可以通过单击单元格查看更多.这是它应该是什么样的(来自Airbnb的应用程序):

在此输入图像描述

我试图通过动态地将UIImageViews添加到表视图单元格中的UIStackView来实现此目的.我正在使用UIStackView,对齐设置为"Fill",分布设置为"Equal spacing",这个想法是堆栈视图将均匀地分隔图标,无论我添加多少.

我在Storyboard中设置了堆栈视图.它对内容视图的顶部,底部,左侧和右侧边距有约束,因此它填充表格视图单元格,直到边距.

这是我用来尝试动态地将UIImageViews添加到单元格的代码.注意amenities是一个字符串数组,等于我的image assets文件夹中的图标名称:

        cell.stackView.backgroundColor = UIColor.greenColor()
        var i = 0
        while (i < amenities.count && i < 4) {
            if (i < 3) {
                let imageView = UIImageView(image: UIImage(named: amenities[i]))
                imageView.contentMode = .ScaleAspectFit
                imageView.translatesAutoresizingMaskIntoConstraints = false
                imageView.backgroundColor = UIColor.blueColor()

                // Add size constraints to the image view
                let widthCst = NSLayoutConstraint(item: imageView, attribute: NSLayoutAttribute.Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 50)
                imageView.addConstraint(widthCst)

                // Add image view to stack view
                cell.stackView.addSubview(imageView) …
Run Code Online (Sandbox Code Playgroud)

ios swift uistackview

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

以编程方式显示UISearchController的搜索栏

注1:这个问题涉及在更新的表视图之外添加UISearchController的搜索栏 - 而不是表视图的标题.

注2:一些反复试验让我找到了解决方案.请参阅下面的答案.

我是iOS开发的新手,并且正在努力使用UISearchController类.我有一个视图控制器,在我的视图控制器视图中,我计划在表视图上方有一个搜索栏.我希望搜索栏链接到UISearchController.由于接口构建器没有UISearchController,我以编程方式添加控制器.在实例化UISearchController之后,我尝试以编程方式将搜索控制器的搜索栏添加到我的视图中但尚未成功.我已经尝试设置搜索栏的框架并给它自动布局约束,但这两种方法都没有对我有效(即当我运行应用程序时,什么都没有出现).这是我尝试过的最新代码:

    let searchController = UISearchController(searchResultsController: nil)

    // Set the search bar's frame
    searchController.searchBar.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 50)

    // Constraint to pin the search bar to the top of the view
    let topConstraint = NSLayoutConstraint(item: searchController.searchBar, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0)
    searchController.searchBar.setTranslatesAutoresizingMaskIntoConstraints(false)

    self.view.addSubview(searchController.searchBar)

    self.view.addConstraint(topConstraint)
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激!谢谢!

编辑:只使用其中一个设置搜索栏的框架或你给它自动布局约束(而不是我最初尝试的组合)似乎首先工作,但点击搜索栏后你会遇到问题指出由德怀特.我已经离开了这些案例的代码,以防它与您现有的内容进行比较,但是对于一个有效的解决方案,请参阅下面的答案.

使用自动布局约束:

    let searchController = UISearchController(searchResultsController: nil)

    let topConstraint = NSLayoutConstraint(item: searchController.searchBar, attribute: NSLayoutAttribute.Top, relatedBy: …
Run Code Online (Sandbox Code Playgroud)

uisearchbar ios programmatically-created swift uisearchcontroller

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

在iOS应用启动时有条件地切换根视图控制器的最佳方法

这里有新的iOS开发者.我正在开发一个项目,该项目要求用户在首次打开应用程序时登录.从那时起,我希望应用程序直接打开应用程序的主流程(在我的情况下是一个标签栏控制器).在做了一些研究之后,我发现了实现这个功能的两种主要方法:

1)有条件地在应用程序委托中设置应用程序窗口的根视图控制器.例如:

    if userLoggedIn {
        let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
        let tabBarController: UITabBarController = storyboard.instantiateViewControllerWithIdentifier("TabBarController") as! UITabBarController
        self.window?.makeKeyAndVisible()
        self.window?.rootViewController = tabBarController
    } else {
        let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
        let logInViewController: LogInViewController = storyboard.instantiateViewControllerWithIdentifier("LogInViewController") as! LogInViewController
        self.window?.makeKeyAndVisible()
        self.window?.rootViewController = logInViewController
    }
Run Code Online (Sandbox Code Playgroud)

2)使用导航控制器作为应用程序的根视图控制器,并有条件地设置由应用程序委托中的导航控制器管理的视图控制器.例如:

    if userLoggedIn {
        let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
        let tabBarController: UITabBarController = storyboard.instantiateViewControllerWithIdentifier("TabBarController") as! UITabBarController

        let navigationController = self.window?.rootViewController as! UINavigationController
        navigationController.navigationBarHidden = true
        navigationController.setViewControllers([tabBarController], animated: true)
    } else …
Run Code Online (Sandbox Code Playgroud)

login ios swift

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

SwiftUI:如何仅剪辑图像的底部

我有一个图像,我希望将其固定到视图顶部,高度为 200。我从以下内容开始:

struct ContentView: View {
    var body: some View {
        VStack {
            Image("frog")
                .resizable()
                .scaledToFill()
                .frame(height:200)

            Spacer()
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这给了我:

在此输入图像描述

您可以看到蓝色轮廓的框架(高度为 200)。现在,我希望图像继续溢出安全区域以填充视图的顶部,就像它正在做的那样。但我想将图像剪裁在其底部框架,所以我得到这样的结果:

在此输入图像描述

我也可以接受这样的事情,其中​​整个图像向上移动到图像的自然底部位于框架底部的位置:

在此输入图像描述

我尝试了多种修饰符,也使用过GeometryReader但未能实现任何结果。我需要它来处理任意尺寸的图像。

image swiftui

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

Swift DateIntervalFormatter:即使间隔跨越多天,也将日期保留在字符串之外

我正在尝试格式化时间间隔。我想要一个看起来像这样的结果:

\n\n
10:45 - 12:00 AM\n
Run Code Online (Sandbox Code Playgroud)\n\n

我可以使用以下方法非常接近这一点DateInvervalFormatter

\n\n
let cal = Calendar.current\nlet formatter = DateIntervalFormatter()\nformatter.dateStyle = .none\nformatter.timeStyle = .short\nlet start = Date()\nlet end = cal.date(byAdding: .hour, value: 1, to: start)\nformatter.string(from: DateInterval(start: start, end: end!))\n
Run Code Online (Sandbox Code Playgroud)\n\n

上面的内容(在 en_US 语言环境中)将产生如下输出:

\n\n
5:27\xe2\x80\x89\xe2\x80\x93\xe2\x80\x896:27 PM\n
Run Code Online (Sandbox Code Playgroud)\n\n

看起来不错吧?但是,如果间隔中的两个日期位于不同日期,则此方法不起作用。例如:

\n\n
let formatter = DateIntervalFormatter()\nformatter.dateStyle = .none\nformatter.timeStyle = .short\nlet startComponents = DateComponents(year: 2020, month: 1, day: 1, hour: 23, minute: 45)\nlet start = cal.date(from: startComponents)\nlet end = cal.date(byAdding: .hour, value: 1, to: start!)\nformatter.string(from: DateInterval(start: …
Run Code Online (Sandbox Code Playgroud)

date-formatting swift

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

Swift Joint 的CombineLatest 不会响应其发布者之一的更新而触发

我正在结合两个发布者来确定地图视图的中心坐标应该是什么。这两家出版商是:

  1. 用户的初始位置由 a 确定CLLocationManager(开始发送位置更新后报告的第一个位置CLLocationManager)。
  2. 如果点击“当前位置的中心地图”按钮,则显示用户的当前位置。

在代码中:

    class LocationManager: NSObject, ObservableObject {

        // The first location reported by the CLLocationManager.
        @Published var initialUserCoordinate: CLLocationCoordinate2D?
        // The latest location reported by the CLLocationManager.
        @Published var currentUserCoordinate: CLLocationCoordinate2D?
        // What the current map view center should be.
        @Published var coordinate: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 42.35843, longitude: -71.05977) // Default coordinate.

        // A subject whose `send(_:)` method is being called elsewhere every time the user presses a button to center the …
Run Code Online (Sandbox Code Playgroud)

swift combinelatest combine

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

Bash:检查是否存在相对路径

我正在编写一个将目录作为唯一参数的 shell 脚本。我需要在做任何其他事情之前检查该目录是否存在。目录可以是绝对的,也可以是相对的。我知道我可以使用

if [ -d "$1"]; then
    # do something if the absolute directory exists
fi
Run Code Online (Sandbox Code Playgroud)

但是,如果工作目录是 /Users/keith/Documents/ 并且我调用我的脚本并只传递“TestFolder”,那么这个 if 语句中的测试将评估为 false,即使 TestFolder 存在于当前工作目录中。

我曾尝试使用将目录转换为绝对目录

abs_path=`cd "$1"; pwd`
Run Code Online (Sandbox Code Playgroud)

然后使用上面的 if 语句测试绝对路径的存在。如果传递给脚本的目录确实存在,则可以正常工作(即使我将其作为参数传递,也可以识别“TestFolder”存在),但如果不存在,则此操作将失败。

我需要的是一种确定作为参数传递的目录是否存在的方法,无论它是作为绝对目录传递还是相对于用户当前工作目录的目录传递,并且如果目录不存在则不会失败.

我显然不是 bash 专家 - 任何建议将不胜感激!

bash shell

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