【问题标题】:reset the UIView when UISearchController is focused当 UISearchController 获得焦点时重置 UIView
【发布时间】:2018-07-02 08:13:01
【问题描述】:

我遇到了一个问题,我可以在我的 UIViewController 中看到黑色部分,因为我的设计不佳,我有 UISearchControllerUICollectionViewUISegmentControl
我使用以下代码在navigationbar 中添加了UISearchController

func setupSearchBar(){

        navigationItem.searchController = taskSearchController
        taskSearchController.searchBar.tintColor = .white
        UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.lightGray]

        UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).attributedPlaceholder = NSAttributedString(string: "Search Text", attributes: [NSAttributedStringKey.foregroundColor: UIColor.lightGray])

        if let textfield = taskSearchController.searchBar.value(forKey: "searchField") as? UITextField {
            if let backgroundview = textfield.subviews.first {
                backgroundview.backgroundColor = UIColor.init(white: 1, alpha: 1)
                backgroundview.layer.cornerRadius = 10
                backgroundview.clipsToBounds = true
            }
        }
        taskSearchController.hidesNavigationBarDuringPresentation = true
        //navigationItem.hidesSearchBarWhenScrolling = false
        //taskSearchController.searchBar.scopeButtonTitles = ["ASSIGNED TASK","CREATED TASK"]
    }

添加UISearchController 后,我将使用以下代码添加UISegmentControlUICollectionView

func setupView(){
        //self.view.addSubview(coverView)
        self.view.addSubview(taskSegment)

    taskSegment.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
    taskSegment.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
    taskSegment.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
    taskSegment.heightAnchor.constraint(equalToConstant: 30).isActive = true

    self.view.addSubview(collectionView)
    collectionView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
    collectionView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
    collectionView.topAnchor.constraint(equalTo: taskSegment.bottomAnchor, constant: 10).isActive = true
    collectionView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: 5).isActive = true

}

这是我最初的看法 这很好。

这是UISearchController 聚焦 时的样子

在这里你可以看到navigationBar 隐藏和UISearchController 放置在顶部的时间我的UISegmentUIcollectionView 保持在相同的位置,所以我想根据UISearchController 的变化移动它。

谁能告诉我该怎么办?

提前谢谢你。

【问题讨论】:

    标签: swift uicollectionview uinavigationbar uisearchcontroller


    【解决方案1】:

    它隐藏了导航栏,因为 hideNavigationBarDuringPresentation 设置为 true。这是预期的行为。

    您需要禁用此功能以使视图约束在演示期间不会发生变化,或者您需要修改约束以使视图适应导航栏的高度变化。通过在主视图的安全区域设置顶部约束,您应该能够很容易地做到这一点。

    【讨论】:

    • 我已经使用自动布局检查func setupView()UISegmentControl设置为视图的安全区域@
    • 在 iOS11 中,搜索栏(设置为navigationItem.searchController = taskSearchController)默认隐藏,向下拖动时显示。那么你有没有做一些事情来覆盖这个?并且以编程方式添加约束时,您应该设置translatesAutoresizingMaskIntoConstraints = false
    • 黑屏是窗口的颜色,如果我将窗口颜色设置为白色,则黑色屏幕变为白色。这解决了一个问题。但是间距问题仍然存在,我需要将 CollectionView 推送到搜索栏。
    猜你喜欢
    • 2011-04-29
    • 1970-01-01
    • 2015-10-30
    • 2013-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-08
    • 2018-02-10
    相关资源
    最近更新 更多