简体   繁体   English

如何以编程方式将 UILabel 放置在 UIImageview 上?

[英]How do I programmatically place UILabel on UIImageview?

override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = .white

//I would like the profile Image View and the username button to be on the topAnchor of the postImageView. //我希望配置文件图像视图和用户名按钮位于 postImageView 的 topAnchor 上。 I don't know how to do this programmatically.我不知道如何以编程方式执行此操作。 Maybe I have to make new constraints but I am not sure how to approach this.也许我必须做出新的限制,但我不知道如何解决这个问题。 I would appreciate it if you could make edits to the code and let me know how to tackle this issue.如果您可以对代码进行编辑并告诉我如何解决此问题,我将不胜感激。

        addSubview(profileImageView)
        profileImageView.anchor(top: topAnchor, left: leftAnchor,
                                paddingTop: 14, paddingLeft: 14)
        profileImageView.setDimensions(height: 58, width: 58)
        profileImageView.layer.cornerRadius = 58 / 2
        
        addSubview(usernameButton)
        usernameButton.centerY(inView: profileImageView,
                               leftAnchor: profileImageView.rightAnchor, paddingLeft: 10)
        
        addSubview(optionsButton)
        optionsButton.centerY(inView: profileImageView)
        optionsButton.anchor(right: rightAnchor, paddingRight: 12)
        
        addSubview(postImageView)
        postImageView.anchor(top: profileImageView.bottomAnchor, left: leftAnchor, right: rightAnchor,
                             paddingTop: 10)
        postImageView.heightAnchor.constraint(equalTo: widthAnchor, multiplier: 1).isActive = true
        
        addSubview(playerView)
        playerView.anchor(top: profileImageView.bottomAnchor, left: leftAnchor, right: rightAnchor,
                          paddingTop: 8)
        playerView.heightAnchor.constraint(equalTo: widthAnchor, multiplier: 1).isActive = true
        
        
        configureActionButtons()
        
        addSubview(likesLabel)
        likesLabel.anchor(top: likeButton.bottomAnchor, left: leftAnchor, paddingTop: -2,
                          paddingLeft: 10)
        
        addSubview(captionLabel)
        captionLabel.anchor(top: usernameButton.bottomAnchor, left: leftAnchor, right: rightAnchor,
                            paddingLeft: 10, paddingRight: 8)
        
        addSubview(postTimeLabel)
        postTimeLabel.anchor(top: captionLabel.bottomAnchor, left: leftAnchor, paddingLeft: 10)
    }

This can be useful to you.这可能对您有用。

https://stackoverflow.com/a/39463981/6849145 https://stackoverflow.com/a/39463981/6849145

Found it on stackoverflow itself.在stackoverflow本身上找到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将UILabel放置在UIImageView下方,以使其始终均匀放置 - How can I place a UILabel below an UIImageView such that it is always evenly placed 如何在单元格之间放置UIImageView? - How do I place UIImageView between cells? 如何以编程方式将 UILabel 居中到 UIView? - How do I center a UILabel to a UIView programmatically? 如何以编程方式在UIScrollView中垂直居中放置UIImageView? - How do I vertically center a UIImageView in a UIScrollView programmatically? 如何从以编程方式创建的UIImageView中选择 - How do I segue from programmatically created UIImageView 如何以编程方式将UIImage添加到UIImageView然后将该UIImageView添加到另一个UIView? - How do I programmatically add a UIImage to a UIImageView then add that UIImageView to another UIView? 如何在UIImageView中添加UILabel - how to add a UILabel in UIImageView Swift 3:如何增加显示为UILabel的变量,然后以编程方式更新UILabel? - Swift 3: How do I increase a variable being displayed as a UILabel, and then update the UILabel programmatically? 在 Swift 4 中以编程方式将 UIImageView、UILabel 添加到 UIStackView - Add UIImageView, UILabel to UIStackView programmatically in Swift 4 使用 uilabel 和 uiimageview 以编程方式为 uitableviewcell 设置约束 - Programmatically set constraints for uitableviewcell with uilabel and uiimageview
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM