Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 以编程方式将UIImage垂直居中于父视图内_Ios_Swift_Uikit - Fatal编程技术网

Ios 以编程方式将UIImage垂直居中于父视图内

Ios 以编程方式将UIImage垂直居中于父视图内,ios,swift,uikit,Ios,Swift,Uikit,我在Swift 5上 目标是将UIImageView垂直居中于视图内。现在看起来像 请注意,所有图像气泡都从单元格中流出 这是导致以下情况的代码: let imageView = UIImageView() let width = self.frame.width let height = self.frame.height let img_width = height //* 0.8 let img_height = height le

我在Swift 5上

目标是将
UIImageView
垂直居中于视图内。现在看起来像

请注意,所有图像气泡都从单元格中流出

这是导致以下情况的代码:

    let imageView = UIImageView()

    let width  = self.frame.width
    let height = self.frame.height

    let img_width  = height //* 0.8
    let img_height = height

    let y = (height - img_height)/2
    let x = width*0.05

    imageView.frame = CGRect(
          x: x
        , y: CGFloat(y)
        , width: img_width
        , height: img_height
    )

    let rounded = imageView
        .makeRounded()
        .border(width:1.0, color:Color.white.cgColor)

    self.addSubview(rounded)
imageView
扩展功能包括:

func makeRounded() -> UIImageView {

    self.layer.borderWidth = 0.5
    self.layer.masksToBounds = false
    self.layer.borderColor = Color.white.cgColor
    self.layer.cornerRadius = self.frame.width/2
    self.clipsToBounds = true

    // see https://developer.apple.com/documentation/uikit/uiview/contentmode
    self.contentMode = .scaleAspectFill

    return self
}

func border( width: CGFloat, color: CGColor ) -> UIImageView{

    self.layer.borderWidth = width
    self.layer.borderColor = color
    return self

}
这是非常香草的

这很奇怪,因为我以完全相同的方式垂直布置文本视图,即:
(parentHeight-childHeight)/2
,并且它居中。您可以在单元格2和单元格3的蓝色文本框中看到它

____编辑_______

这就是我布置牢房的方式

        let data = dataSource[ row - self._data_source_off_set ]

        let cell  = tableView.dequeueReusableCell(withIdentifier: "OneUserCell", for: indexPath) as! OneUserCell

        // give uuid and set delegate
        cell.uuid = data.uuid
        cell.delegate = self

        // render style: this must be set
        cell.hasFooter = false //true

        cell.imageSource      = data
        cell.headerTextSource = data
        cell.footerTextSource = data

        // color schemes
        cell.backgroundColor = Color.offWhiteLight
        cell.selectionColor = Color.graySecondary

将这些约束添加到imageView并删除框架及其计算

self.contentView.addSubview(rounded)
self.mimageView.translatesAutoresizingMaskIntoConstraints = false
self.mimageView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor,constant: 20).isActive = true
self.mimageView.centerYAnchor.constraint(equalTo: contentView.centerYAnchor).isActive = true
self.mimageView.heightAnchor.constraint(equalTo: contentView.heightAnchor).isActive = true
self.mimageView.widthAnchor.constraint(equalTo: contentView.heightAnchor).isActive = true

显示设置单元格的UITableViewCell类您正在使用tableView。。。牢房有多高?@jawadAli是的,这是一个桌面视图。高度是90。我在情节提要中指定了高度。其余的都是代码。不理想,但我现在正在试验。你没有使用约束吗?@jawadAli对ios来说非常陌生,所以没有。但是如果有人能提供一个使用约束的解决方案,那就很理想了。好的,我明白了,当我指定单元格高度时,我在故事板中使用的高度与在表格中使用的高度不同。这些约束也将图像水平放置在单元格的中心。。它们消除了makeRounded效应。此外,由于明显的原因,我无法使用CGFloat设置CenterXacho约束。