ios – 自定义导航栏项目放置不当

ios – 自定义导航栏项目放置不当,第1张

概述我有一个自定义导航栏子类: class ProfileNavigationBar: UINavigationBar { var titleLabel: UILabel var backButton: UIBarButtonItem var friendsButton: FriendsButton? required init?(coder: NSCoder) { tit 我有一个自定义导航栏子类:

class ProfileNavigationbar: UINavigationbar {  var TitleLabel: UILabel  var backbutton: UIbarbuttonItem  var frIEndsbutton: FrIEndsbutton?  required init?(coder: NSCoder) {    TitleLabel = UILabel(frame: CGRect(x: 0,y: 40,wIDth: 320,height: 40))    backbutton = UIbarbuttonItem.backbutton(nil,action: nil)    frIEndsbutton = FrIEndsbutton(frame: CGRect(x: 0,y: 0,wIDth: 24,height: 24))    super.init(coder: coder)  }  overrIDe func awakeFromNib() {    super.awakeFromNib()    let item = UINavigationItem()    item.TitleVIEw = TitleLabel    item.leftbarbuttonItem = backbutton    item.hIDesBackbutton = true    let frIEndsItem = UIbarbuttonItem(customVIEw: frIEndsbutton!)    item.rightbarbuttonItems = [frIEndsItem]    pushItem(item,animated: false)  }}

FrIEndsbutton在更改状态属性时自行调整大小的位置.

问题是当第一次加载视图时,它看起来像这样,后退按钮和FrIEndsbutton就在导航栏的边缘:( .loading state)

但是,当我将FrIEndsbutton状态更改为.add时,通常会显示如下:

我怎样才能解决这个问题?

这是FrIEndsbutton的实现:

class FrIEndsbutton: UIVIEw {  var state: FrIEndsbuttonState {    dIDSet {      style(selected: state)    }  }  var Title: String = "" {    dIDSet {      set(Title: Title)    }  }  var Font = UIFont.boldSystemFont(ofSize: 11)  private var imageVIEw: UIImageVIEw!  private var button: UIbutton!  var loading: UIActivityIndicatorVIEw!  init(frame: CGRect,state: FrIEndsbuttonState = .loading) {    self.state = state    super.init(frame: frame)    backgroundcolor = .yellow    let plusSize = frame.size.height/2    let plusYValue = (frame.size.height-plusSize)/2    imageVIEw = UIImageVIEw(frame: CGRect(x: plusYValue*2,y: plusYValue,wIDth: plusSize,height: plusSize))    imageVIEw.contentMode = .scaleAspectFit    addSubvIEw(imageVIEw)    let TitleSize = (Title as Nsstring).size(attributes: [NSFontAttributename : Font])    button = UIbutton(frame: CGRect(x: plusYValue*3.5 + plusSize,wIDth: TitleSize.wIDth,height: frame.size.height))    addSubvIEw(button)    loading = UIActivityIndicatorVIEw(activityIndicatorStyle: .gray)    loading.center = center    addSubvIEw(loading)    style(selected: state)    updateSize()  }  func addTarget(object: Any,selector: Selector) {    button.addTarget(object,action: selector,for: .touchUpInsIDe)  }  required init?(coder aDecoder: NSCoder) {    fatalError("init(coder:) has not been implemented")  }  private func style(selected: FrIEndsbuttonState) {    configureborder(state: selected)    loading.startAnimating()    loading.isHIDden = state != .loading    isHIDden = false    switch state {    case .frIEnds:      backgroundcolor = .black      button.setTitlecolor(.white,for: .normal)      imageVIEw.image = #imageliteral(resourcename: "frIEnds-tick")      Title = "FrIEnds"    // ... + all other cases    }    self.updateSize()  }  private func configureborder(state: FrIEndsbuttonState) {    layer.bordercolor = UIcolor.black.cgcolor    layer.borderWIDth = state == .loading ? 0 : 1    layer.cornerRadius = 5  }  private func set(Title: String) {    let plusSize = frame.size.height/2    let plusYValue = (frame.size.height-plusSize)/2    let TitleSize = (Title as Nsstring).size(attributes: [NSFontAttributename : Font])    button.TitleLabel?.Font = Font    button.setTitle(Title,for: .normal)    button.frame = CGRect(x: plusYValue*3.5 + plusSize,height: frame.size.height)    self.updateSize()  }  private func updateSize() {    if state == .loading {      frame.size.wIDth = frame.size.wIDth      loading.center = CGPoint(x: frame.size.wIDth/2,y: frame.size.height/2)      loading.startAnimating()      return    }    let plusSize = frame.size.height/2    let plusYValue = (frame.size.height-plusSize)/2    let TitleSize = (Title as Nsstring).size(attributes: [NSFontAttributename : Font])    let totalWIDth = plusYValue*5.5 + plusSize + TitleSize.wIDth    frame.size.wIDth = totalWIDth  }

编辑:我已经尝试将按钮设置为最初的.add状态,但它仍然会出现在导航栏的最右侧,直到它被更改为其他状态.似乎按钮的第一个状态总是使导航栏将其所有子项移动到帧的边缘,直到它被更新.

编辑:我无法通过复制相关代码在另一个项目上重现问题,但这是我遇到的具体问题(如下图所示).首次导航到视图时,导航栏边缘和后退按钮之间的间隙不会保持(我设法在导航推送动画的中途获得屏幕截图).我现在的问题是,是什么导致这种情况?

解决方法 你必须停止活动指示器,否则它不会隐藏自己:

private func style(selected: FrIEndsbuttonState) {    configureborder(state: selected)    if (state == .loading) {        loading.startAnimating()    } else {        loading.stopAnimating()    }    loading.isHIDden = state != .loading

顺便说一句:你也可以跳过loading.isHIDden = state!= .loading如果你将UIActivityIndi​​catorVIEw配置成在它停止时隐藏自己:

init(frame: CGRect,state: FrIEndsbuttonState = .loading) {     // ...    loading = UIActivityIndicatorVIEw(activityIndicatorStyle: .gray)    loading.center = center    loading.hIDesWhenStopped = true    addSubvIEw(loading)    // ...}

要正确布置右侧导航栏按钮,您必须修改FrIEndsbutton.updateSize:当处于状态.loading时 – 至少是第一次 – 您还必须更新框架.

private func updateSize() {    if state == .loading {        frame.size.wIDth = frame.size.wIDth        loading.center = CGPoint(x: frame.size.wIDth/2,y: frame.size.height/2)        loading.startAnimating()        // do not return here    }    let plusSize = frame.size.height/2    let plusYValue = (frame.size.height-plusSize)/2    let TitleSize = (Title as Nsstring).size(attributes: [NSFontAttributename : Font])    let totalWIDth = plusYValue*5.5 + plusSize + TitleSize.wIDth    frame.size.wIDth = totalWIDth}

我假设左侧按钮相同,但不幸的是你的代码不能在这里编译(在调用backbutton = UIbarbuttonItem.backbutton(nil,action:nil)时在init?方法中得到一个错误,因为没有静态成员backbutton on的UIbarbuttonItem

现在,it works on my machine:

在应用程序启动后:

状态改为.frIEnds后

国家又改回了.loading

一切都很好,没有变化等.如果这不适合您的项目,那么您可能还有其他方面没有与您的代码一起发布.

总结

以上是内存溢出为你收集整理的ios – 自定义导航栏项目放置不当全部内容,希望文章能够帮你解决ios – 自定义导航栏项目放置不当所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1209530.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存