简体   繁体   中英

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. 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

Found it on stackoverflow itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM