Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.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 使用“自动布局”在UIView中定位定位子视图的百分比_Ios_Swift_Autolayout_Nslayoutconstraint - Fatal编程技术网

Ios 使用“自动布局”在UIView中定位定位子视图的百分比

Ios 使用“自动布局”在UIView中定位定位子视图的百分比,ios,swift,autolayout,nslayoutconstraint,Ios,Swift,Autolayout,Nslayoutconstraint,我正在使用一些JSON,它为我提供了一系列视图和子视图,以便在UIView中进行布局,我希望使用AutoLayout来实现它。以下是JSON的一个示例: "component": { "width": 150, "height": 100, "elements": [ { "x": 0.1, "y": 0.1, "w": 0.8, "h": 0.8

我正在使用一些JSON,它为我提供了一系列视图和子视图,以便在
UIView
中进行布局,我希望使用AutoLayout来实现它。以下是JSON的一个示例:

"component": {
    "width": 150,
    "height": 100,
    "elements": [
        {
            "x": 0.1,
            "y": 0.1,
            "w": 0.8,
            "h": 0.8
        }   
    ]
}
在本例中,外部视图(组件视图)要求尺寸为150 x 100。我得到了基于屏幕宽度保持纵横比的布局,如下所示:

private func fittedSize(for dimensions: Dimensions) -> CGSize {
    var width: CGFloat
    var height: CGFloat

    if dimensions.width.value > dimensions.height.value {
        let aspectRatio = dimensions.height.value / dimensions.width.value
        width = dimensions.width.value
        height = dimensions.width.value * aspectRatio
    } else {
        let aspectRatio = dimensions.width.value / dimensions.height.value
        height = dimensions.height.value
        width = dimensions.height.value * aspectRatio


    let apertureSize = CGSize(width: width, height: height)

    if apertureSize.width > apertureSize.height {
        let scale = bounds.width / apertureSize.width
        return CGSize(width: apertureSize.width * scale, height: apertureSize.height * scale)
    } else {
        let scale = bounds.height / apertureSize.height
        return CGSize(width: apertureSize.width * scale, height: apertureSize.height * scale)
    }
}
上述函数计算视图的最大大小,同时保持其尺寸指定的纵横比,因此,例如,在屏幕宽度为320时,100x150视图将为320x480

现在我需要布局该视图的子视图,其中包含该组件中的元素。这些值指定为百分比,因此在上面的示例中,元素的位置应为从左到上的宽度的10%,以及superview的宽度和高度的80%

我可以使用带乘数的约束轻松获得正确的宽度和高度,如下所示:

let w = elementView.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: element.position.width)
let h = elementView.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: element.position.height)
这将取0.8,并使子视图的宽度和高度为其父视图的80%。问题在于,我还想对其位置(x和y)使用乘数。AutoLayout不支持这一点,这就是我要寻找的答案

如何使用百分比值,使用自动布局而不是手动定位帧,在另一个
UIView
中定位一个帧并调整其大小?

注:

下面是一个关于当前情况的示例。红色区域是子视图,淡蓝色区域是超级视图。请注意,子视图的大小是正确的,是宽度和高度的80%,但它没有显示从顶部和左侧进入的10%,这就是我缺少的


您还需要给出顶部和左侧约束。 像下面这样

elementView.topAnchor.constraint(equalTo: view.topAnchor, constant: height / 10),
elementView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: width / 10),

您可以使用UILayoutGuide或虚拟视图,约束超级视图的前缘和视图的前缘,并使用乘数将此“间隔”视图宽度约束为其超级视图宽度

设l=view.leadingAnchor.constraint(等式:view.leadingSpacer.leadingAnchor) 设w=leadingSpacer.widthAnchor.constraint(等式:view.widthAnchor,乘数:element.position.x) 设t=leadingSpacer.trailingAnchor.constraint(to:elementView.leadingAnchor)

并激活此约束

对顶部垫片也做类似的操作。
对于这个问题,我使用了虚拟视图,但UILayoutGuide被认为是更有效的解决方案,因为它只与AutoLayout引擎交互,不参与渲染。希望这能有所帮助。

您还需要约束此间隔的视图顶部和底部锚定,以使布局明确无误