gpt4 book ai didi

ios - UINavigationBar contentMode 未设置

转载 作者:行者123 更新时间:2023-11-30 12:12:08 24 4
gpt4 key购买 nike

我正在尝试将 UINavigationBar 的背景设为图像,但该图像并不是栏的确切大小。由于某种原因,我似乎无法正确设置栏的 contentMode 。这是我的代码:

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

// ...


let image = UIImage(named: object?.imageName ?? "")
navigationController?.navigationBar.alpha = 0
navigationController?.navigationBar.setBackgroundImage(image, for: .default)
self.navigationController?.navigationBar.contentMode = .scaleAspectFill
UIView.animate(withDuration: Double(UINavigationControllerHideShowBarDuration), animations: {
self.navigationController?.navigationBar.alpha = 1
})
}

我不知道我做错了什么,或者也许 contentModeUINavigationBar 没有做任何事情。

当前发生的情况是背景图像再次呈现为马赛克

谢谢

最佳答案

您可以创建 UIView 并向其添加图像和文本,但必须设置约束

    let titleView = UIView()
titleView.frame = CGRect(x: 0, y: 0, width: 100, height: 40)

let containerView = UIView()
containerView.translatesAutoresizingMaskIntoConstraints = false

titleView.addSubview(containerView)

let profileImageView = UIImageView()
profileImageView.translatesAutoresizingMaskIntoConstraints = false
profileImageView.contentMode = .scaleAspectFill
profileImageView.layer.cornerRadius = 20
profileImageView.clipsToBounds = true
containerView.addSubview(profileImageView)

profileImageView.leftAnchor.constraint(equalTo: containerView.leftAnchor).isActive = true
profileImageView.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
profileImageView.widthAnchor.constraint(equalToConstant: 40).isActive = true
profileImageView.heightAnchor.constraint(equalToConstant: 40).isActive = true

self.navigationItem.titleView = titleView

titleView.addGestureRecognizer(UITapGestureRecognizer(target:
self, action: #selector(func)))

希望对你有帮助

关于ios - UINavigationBar contentMode 未设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45924315/

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