【问题标题】:Constrain Button with its own width用自己的宽度约束 Button
【发布时间】:2020-07-22 12:26:13
【问题描述】:

真的很难constrain 我的buttons

我想要的只是将我的 2 个buttons 设置为有一个 35height 并且他们的width 应该是他们需要的任何东西。现在它看起来像这样(左右按钮):

这是我设置它们的方式:

let communityButton: UIButton = {
    let v = UIButton()
    v.setImage(UIImage(systemName: "person.3.fill"), for: .normal)
    v.tintColor = UIColor.darkCustom
    v.imageView?.contentMode = .scaleAspectFill
    v.contentHorizontalAlignment = .fill
    v.contentVerticalAlignment = .fill
    v.translatesAutoresizingMaskIntoConstraints = false
    v.addTarget(self, action: #selector(communityButtonTapped), for: .touchUpInside)
    return v
}()

let profileButton: UIButton = {
    let v = UIButton()
    v.setImage(UIImage(systemName: "person.fill"), for: .normal)
    v.tintColor = UIColor.darkCustom
    v.imageView?.contentMode = .scaleAspectFill
    v.contentHorizontalAlignment = .fill
    v.contentVerticalAlignment = .fill
    v.translatesAutoresizingMaskIntoConstraints = false
    v.addTarget(self, action: #selector(profileButtonTapped), for: .touchUpInside)
    return v
}()

约束:

//contrain communityButton
communityButton.centerYAnchor.constraint(equalTo: bottomBar.centerYAnchor),
communityButton.centerXAnchor.constraint(equalTo: bottomBar.centerXAnchor, constant: -view.frame.width/3.5),
communityButton.heightAnchor.constraint(equalToConstant: 35),

// constrain profileButton
profileButton.centerYAnchor.constraint(equalTo: bottomBar.centerYAnchor),
profileButton.centerXAnchor.constraint(equalTo: bottomBar.centerXAnchor, constant: view.frame.width/3.5),
profileButton.heightAnchor.constraint(equalToConstant: 35),

这里的正确约束方式是什么?

【问题讨论】:

    标签: ios swift uibutton constraints


    【解决方案1】:

    您可能需要让 autoLayout 知道您希望宽度灵活。你可以通过在你的两个按钮定义上添加这一行来做到这一点: -

    // Replace "myButton" with your buttons name in your case "v"
     myButton.autoresizingMask = [.flexibleWidth] 
    

    编辑:- 由于您使用的是系统图像,如果您希望按钮看起来更大,那么您必须增加图像配置的字体大小。通过使用您喜欢的字体大小添加配置来编辑您的图像。

    例如:-

    
    let communityButton: UIButton = {
        let v = UIButton()
    //----
    // NB: - Change the font size for bigger icons and viceversa
    let imageSymbolConfiguration = UIImage.SymbolConfiguration(pointSize: 50, weight: .regular, scale: .large)
    v.setImage(UIImage(systemName: "person.3.fill", withConfiguration: imageSymbolConfiguration), for: .normal)
    
    // -----
    
        return v
    }()
    
    
    let profileButton: UIButton = {
        let v = UIButton()
        // ----
    // NB: - Change the font size for bigger icons and viceversa
    let imageSymbolConfiguration = UIImage.SymbolConfiguration(pointSize: 50, weight: .regular, scale: .large)
    v.setImage(UIImage(systemName: "person.fill", withConfiguration: imageSymbolConfiguration), for: .normal)
         // ----
        return v
    }()
    
    
    

    解释:-

    来自official documentation

    “符号图像配置对象包括应用于符号图像的点大小、比例、文本样式、粗细和字体等详细信息。系统使用这些详细信息来确定要使用的图像变体以及如何缩放或设置图像样式。”

    【讨论】:

    • 那没有任何东西:(我需要改变其他东西吗?
    • 嘿,@Chris 我编辑了我的答案,请再检查一次。
    • 这似乎有效! pointSizeweight & scale
    • 我在答案的底部添加了解释。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-24
    • 2015-01-16
    • 2017-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多