gpt4 book ai didi

swift MaterialComponents.MDCTabBarView mdc_customView 不能正常工作

转载 作者:行者123 更新时间:2023-12-03 20:39:18 27 4
gpt4 key购买 nike

我正在尝试将 customView 添加到 MDCTabBarItem 使用 mdc_customView 但项目没有采用正确的宽度,结果如下
enter image description here
如果我不设置 mdc_customView value 那么结果是预期的,但没有定制设计
enter image description here
带有 mdc_customView 的代码

 override func parseTabBarItems(data: [SubCategory]) -> [MDCTabBarItem] {
var result: [MDCTabBarItem] = []
var nextX: CGFloat = 15
for cat in data {
guard let count = cat.sub?.count, count > 0 else { continue }
let item = MDCTabBarItem()
item.tag = result.count

let customeView = MDCTabBarCustomView()
customeView.frame = CGRect(x: nextX, y: 0, width: (cat.ref ?? "").sizeOfString(usingFont: .ttrSemiBold10).width, height: 50)
nextX = nextX + 15 + (cat.ref ?? "").sizeOfString(usingFont: .ttrSemiBold10).width
customeView.config(title: cat.ref ?? "")
item.mdc_customView = customeView
result.append(item)
}
return result
}
没有 mdc_customView 的代码
override func parseTabBarItems(data: [SubCategory]) -> [MDCTabBarItem] {
var result: [MDCTabBarItem] = []
var nextX: CGFloat = 15
for cat in data {
guard let count = cat.sub?.count, count > 0 else { continue }
let item = MDCTabBarItem(title: cat.ref ?? "", image: nil, tag: result.count)
result.append(item)
}
return result
}
MDCTabBarCustomView
import UIKit
import MaterialComponents.MDCTabBarView
class MDCTabBarCustomView: UIView , MDCTabBarViewCustomViewable {


var titleLabel: UILabel!
var containerView: UIView!

var contentFrame: CGRect
init() {
self.titleLabel = UILabel.newAutoLayout()
self.containerView = TTRView.newAutoLayout()
self.contentFrame = .zero
super.init(frame: .zero)
self.autoresizingMask = []
self.setup()
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

func config(title: String) {
self.titleLabel.text = title
}

func setSelected(_ selected: Bool, animated: Bool) {}

private func setup(){
self.addSubview(self.containerView)
self.containerView.addSubview(self.titleLabel)

self.containerView.snp.makeConstraints{
$0.edges.equalToSuperview()
}

self.titleLabel.snp.makeConstraints{
$0.edges.equalToSuperview().offset(5)
}
}
}
tabBar 设置:
 self.tabBar.preferredLayoutStyle = .scrollable

最佳答案

在花了一整天的时间尝试和学习这个新的 customView 之后,我能够使它工作,下面是工作代码
都是关于内在内容大小 layoutSubviews
这是新的输出
enter image description here

final class MDCTabBarCustomView: UIView , MDCTabBarViewCustomViewable {
var contentFrame: CGRect {
return self.titleLabel.frame
}

var titleLabel: UILabel!
var containerView: UIView!
init(){
self.titleLabel = UILabel.newAutoLayout()
self.containerView = UIView.newAutoLayout()
super.init(frame: .zero)
self.autoresizingMask = []
}

override func layoutSubviews() {
super.layoutSubviews()
if self.containerView.superview != self {
self.addSubview(self.containerView)
}
if self.titleLabel.superview != self.containerView {
self.containerView.addSubview(self.titleLabel)
}

containerView.snp.makeConstraints{
$0.top.leading.equalToSuperview().offset(5)
$0.bottom.trailing.equalToSuperview().offset(-5)
}
titleLabel.snp.makeConstraints{
$0.top.equalToSuperview().offset(5)
$0.bottom.equalToSuperview().offset(-5)
$0.centerX.equalToSuperview()
}
}
override var intrinsicContentSize: CGSize {
return CGSize(width: self.titleLabel.intrinsicContentSize.width + 20, height: self.titleLabel.intrinsicContentSize.height + 20)
}

}

关于swift MaterialComponents.MDCTabBarView mdc_customView 不能正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67720137/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com