简体   繁体   English

我如何完全隐藏这个UIImageView

[英]How do I completely hide this UIImageView

I have a UITableView with a list of items that can be filtered. 我有一个UITableView其中包含可以过滤的项目列表。 When the user chooses a filter, a UIView is exposed using a height NSLayoutConstraint that changes from 0 to 40 pixels: 当用户选择过滤器时,使用从0到40像素更改的高度NSLayoutConstraint公开UIView:

在此输入图像描述

The circle with the X is the clear button that clears the filter and then closes the UIView by changing the height constraint back to 0. 带X的圆圈是清除按钮,清除过滤器,然后通过将高度约束更改回0来关闭UIView。

The problem is that when the UIView closes, that clear button doesn't completely go away: 问题是,当UIView关闭时,该清除按钮不会完全消失:

在此输入图像描述

Here's the relevant code: 这是相关的代码:

class LiftLogViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, NSFetchedResultsControllerDelegate {

  let coreDataStack = CoreDataStack()

  var liftEvents = [LiftEvent]()

  //MARK: IB outlets

  @IBOutlet var tableView: UITableView!
  @IBOutlet weak var navItem: UINavigationItem!
  @IBOutlet weak var filterViewHeightConstraint: NSLayoutConstraint!
  @IBOutlet weak var clearFilterButton: UIImageView!
  @IBOutlet weak var selectedFilter: UILabel!
  @IBOutlet weak var clearButtonHeightConstraint: NSLayoutConstraint!
  @IBOutlet weak var clearButtonView: UIImageView!

  var isFilterViewOpen = false

  override func viewDidLoad() {

    let doneButton = UIBarButtonItem(title: "Done", style: .Plain, target: self, action: #selector(self.dismissLog(_:)))

    let filterImage = UIImage(named: "filter_icon")

    let filterButton = UIBarButtonItem(image: filterImage, style: .Plain, target: self, action: #selector(self.actionFilter))

    self.navItem.rightBarButtonItems = [doneButton, filterButton]

    let buttonTap = UITapGestureRecognizer(target: self, action: #selector(self.clearFilter))

    clearFilterButton.addGestureRecognizer(buttonTap)

    filterViewHeightConstraint.constant = 0.0

    clearButtonHeightConstraint.constant = 0.0

    super.viewDidLoad()
  }


override func viewWillAppear(animated: Bool) {
    let filterPredicate: NSPredicate?
    if let logFilter = UserDefaultsManager.sharedInstance.logFilter?.rawValue {
      filterPredicate = NSPredicate(format: "lift.liftName = [c] %@", logFilter)
      print("viewWillAppear thinks the filter is \(logFilter)")
    } else {
      filterPredicate = nil
    }
    reloadData(filterPredicate)

    let currentFilter = getCurrentLogFilter()

    if currentFilter != nil {
      selectedFilter.text = "Filtered by \(currentFilter!)"
      isFilterViewOpen = true
      clearButtonView.hidden = isFilterViewOpen ? false : true
    } else {
      selectedFilter.text = nil
    }

    super.viewWillAppear(animated)
  }


 override func viewDidAppear(animated: Bool) {
    filterViewHeightConstraint.constant = isFilterViewOpen ? 40.0 : 0.0

    clearButtonHeightConstraint.constant = isFilterViewOpen ? 21.0 : 0.0

    clearButtonView.hidden = isFilterViewOpen ? false : true

    UIView.animateWithDuration(0.33, delay: 0, options: [.CurveEaseOut], animations: {

    self.view.layoutIfNeeded()
        }, completion: nil)
  }


func clearFilter() {
    UserDefaultsManager.sharedInstance.logFilter = nil

    isFilterViewOpen = !isFilterViewOpen

    UIView.animateWithDuration(0.33, delay: 0, options: [.CurveEaseOut], animations: {

      self.view.layoutIfNeeded()
      }, completion: nil)

    selectedFilter.text = nil

    reloadData()
  }

You can see I've tried both setting the .hidden value on the UIView that holds the clear button to true and I've tried changing the height constraint to 0.0 but neither of those make it gone completely. 你可以看到我已经尝试在UIView上设置.hidden值,将clear按钮设置为true,并且我尝试将高度约束更改为0.0,但这两者都没有完全消失。

Searching for a while hasn't produced any answers. 搜索一段时间没有产生任何答案。 Can anybody point out what's wrong with what I'm doing? 任何人都可以指出我正在做的事情有什么问题吗? Thanks. 谢谢。

It seems like the hair line color on the navigation bar may be clear resulting in the button still being slightly visible. 看起来导航栏上的发线颜色可能很清晰,导致按钮仍然略微可见。 Maybe change that and see if it covers the button. 也许更改它,看它是否覆盖按钮。 You can do so like this: 你可以这样做:

let view = UIView()
view.backgroundColor = UIColor.grayColor()
self.navigationController!.navigationBar.hairlineImageViewInNavigationBar(view)

As Dustin Spengler mentioned, the transparent hairline image view in the navigation bar causes the filter button to be slightly visible beneath it. 正如Dustin Spengler所提到的,导航栏中的透明细线图像视图会使滤镜按钮在其下方略微可见。 As a workaround, you can hide the filter view ( filterView.hidden = true ) when constraint constant is set to 0.0 and unhide the filter view ( filterView.hidden = false ) when constraint constant is set to 40.0. 作为一种变通方法,可以隐藏的过滤视图(filterView.hidden =真 )时约束常数被设定为0.0和取消隐藏过滤视图(filterView.hidden =假 )时约束常数设置为40.0。

Thank you guys for your suggestions. 谢谢你们的建议。 It turns out that the solution was a little different, but your suggestions got me to poke around some more in that area and find the solution. 事实证明,解决方案有点不同,但你的建议让我在该领域找到了更多的解决方案。

I neglected to mention that this was a view controller that I add a UIStackView that wrapped my UIView (the grey "filtered by" UIView ) and the UITableView : 我忽略了提到这是一个视图控制器,我添加了一个UIStackView ,它包裹了我的UIView (灰色的“过滤了” UIView )和UITableView

在此输入图像描述

The problem was that the constraint I created from the top of the UIStackView to the top of its parent view was just slightly too big, pushing the top of the UIStackView just far enough to expose that little sliver. 问题是我从UIStackView顶部到其父视图顶部创建的约束只是略微过大,将UIStackView的顶部推得足够远以暴露那个小条子。 I reduced the Y distance to 63 and it's now perfect: 我将Y距离减少到63,现在它完美了:

在此输入图像描述

Thanks again for trying to help. 再次感谢您的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM