首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未设置UINavigationBar contentMode

未设置UINavigationBar contentMode
EN

Stack Overflow用户
提问于 2017-08-29 01:21:03
回答 1查看 194关注 0票数 0

我正在尝试使UINavigationBar的背景成为一个图像,而该图像并不是条形图的确切大小。由于某些原因,我似乎无法正确设置工具栏的contentMode。这是我的代码:

代码语言:javascript
复制
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
    })
}

我不知道我做错了什么,或者也许contentMode根本没有为UINavigationBar做任何事情。

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

谢谢

EN

回答 1

Stack Overflow用户

发布于 2017-08-29 02:43:40

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

代码语言:javascript
复制
    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)))

我希望我能帮到你

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45924315

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档