无法为ImageView设置右框架

时间:2019-02-16 10:58:14

标签: swift frame

我无法将UIImageView设置为控制器上部的tableview.backgroundview的一半大小。

我试图将头像的原点设置为原点视图,但这不起作用。

    let avatar =  UIImageView(frame: self.view.frame)
    let imgageOfAvatar = UIImage(named: "avatar.jpg")
    avatar.image = imgageOfAvatar

   avatar.frame = CGRect(x: self.view.frame.origin, y: self.view.frame.origin , width: self.view.frame.width, height: self.view.frame.height / 2)
    avatar.frame.origin = self.view.frame.origin

    //avatar.contentMode = UIViewContentMode.scaleAspectFill
    self.tableView.addSubview(avatar)
    self.tableView.sendSubview(toBack: avatar)

https://pp.userapi.com/c847120/v847120175/19f9cf/128kU2yWq-E.jpg

3 个答案:

答案 0 :(得分:0)

尝试一下

avatar.frame.origin.x = self.view.frame.origin.x avatar.frame.origin.y = self.view.frame.origin.y

avatar.frame = CGRect(来源:self.view.frame.origin,大小:宽度:self.view.frame.width,高度:self.view.frame.height / 2)

答案 1 :(得分:0)

您应该将其添加到这样的视图中:

    let avatar =  UIImageView(frame: self.view.frame)
    let imgageOfAvatar = UIImage(named: "avatar.jpg")
    avatar.image = imgageOfAvatar

    avatar.frame = CGRect(x: self.view.frame.origin.x, y: self.view.frame.origin.y , width: self.view.frame.width, height: self.view.frame.height / 2)
    avatar.frame.origin = self.view.frame.origin

    self.view.insertSubview(avatar, at: 0)

答案 2 :(得分:0)

我得到了预期的结果 output

    let avatar = UIImageView(frame: self.view.frame)
    let imgageOfAvatar = UIImage(named: "dog.jpeg")
    avatar.image = imgageOfAvatar
    avatar.frame = CGRect(origin: self.view.frame.origin,
                          size: CGSize(width: self.view.frame.width,
                                       height: self.view.frame.height / 2))
    self.tableview.addSubview(avatar)
    self.tableview.sendSubviewToBack(avatar)