UINavigationBar.appearance()。半透明正在创建一个大的空白区

时间:2016-08-29 04:57:19

标签: ios swift uiappearance

更新2 : 我仍然希望能够帮助解决这个问题,所以我现在要稍微改进一下我的问题,我已经把它缩小了。

我的UIViewController层次结构如下所示:

enter image description here

如果用户触摸过滤器图标并选择升降机类型,则会显示过滤器视图。左侧的图像是未选择过滤器的样子,右侧图像是选择过滤器时的样子。导航区域(以绿色标出)是我想要设计的样式。

enter image description here

这是我无法弄清楚的事情。如果我添加这两件事:

UINavigationBar.appearance().translucent = false
UINavigationBar.appearance().barTintColor = UIColor(hexString: "232B35")

然后导航区域和过滤器视图之间出现空白区域:

enter image description here

如果我将半透明属性设置为true,则不会出现该空格,但barTintColor无效。

请注意,我没有UINavigationBar中添加UIViewController。我只有一个UINavigationItem我正在添加代码。

在这一点上,我的问题是这样的 - 当我的代码中有UINavigationBar.appearance().translucent = false时,为什么大的空白区域突然出现(我可以做些什么来摆脱它,但仍然改变颜色我的导航区域)?

我真的被困住了,会感激一些帮助。谢谢!

以下是我原来的问题,其中包含的信息可能有用,也可能没有帮助: 我正在尝试设计我的应用程序并且遇到困难时间。在一个UIViewController我有一个ThemeManager struct,其中包含一个将主题应用到应用程序的函数,以及使导航栏与应用程序的背景颜色融为一体的方法代码:

static func applyTheme(theme: Theme) {

    // set the background color
    sharedApplication.delegate?.window??.backgroundColor = UIColor(hexString: "232B35")

    UINavigationBar.appearance().translucent = false // these are the offenders
    UINavigationBar.appearance().barTintColor = UIColor(hexString: "232B35")


    // first, set backgroundimage to nothing
    UINavigationBar.appearance().setBackgroundImage(
      UIImage(),
      forBarPosition: .Any,
      barMetrics: .Default)

    // and also set shadowimage to nothing
    UINavigationBar.appearance().shadowImage = UIImage()

    UIStatusBarStyle.LightContent
    UILabel.appearance().textColor = UIColor(hexString: "768DAA")
    }
  }

我的初始ViewController看起来像我想要的那样 - 背景和导航组件颜色相同:

enter image description here

但是在不同的视图控制器上,它有一个我不想要的大空间:

enter image description here

我已经发现导致这种情况发生的是UINavigationBar.appearance().translucentUINavigationBar.appearance().barTintColor属性。就像在实际导航区域下面添加UINavigationBar一样。如果我对该代码进行评论,它当然没有颜色,但我不想要的大空间也消失了:

enter image description here

两个VC之间的最大区别在于:在第一个看起来像我想要的VC中,我故意通过故事板放置UINavigationBar但是在第二个VC中我没有。我只在那个中放了一个UINavigationItem。另请注意,第二个VC包含UITableView,但它不是UITableViewController

如何在导航区域内获得我想要的纯色而不在其下方添加这些不需要的空间?

编辑#1 以下是UIViewController的问题:

enter image description here

请注意,层次结构是带有@IBOutlet连接的UIView,低于UIStackView,其中包含使用高度布局约束进行展开和折叠的过滤器视图(UIView),和UITableView。因此,出现的大空白区域甚至不在我的层次结构中。

如果它有帮助,这是提升日志viewDidLoad中的UIViewController方法:

类LiftLogViewController:UIViewController,UITableViewDataSource,UITableViewDelegate,NSFetchedResultsControllerDelegate {

//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!

  let coreDataStack = CoreDataStack()
  var liftEvents = [LiftEvent]()
  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()
  }

1 个答案:

答案 0 :(得分:0)

您是否通过情节提要将第一个VC UINavigationBar属性中的translucent设置为false,以使其与UINavigationBar.appearance()保持一致?

enter image description here

然后你的第二个VC Top Bar应该从第一个VC推断。

enter image description here