如何在Swift上使用框架来设置循环的X位置[英] How to use frame to set x position in loop on Swift

本文是小编为大家收集整理的关于如何在Swift上使用框架来设置循环的X位置的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我的数组数在1到3之间. 我想在一定距离的屏幕中间将它们构架 标签的宽度也固定

let array = ["some1", "some2", "some3"]

func setLabel(){
  var i = -1
  for text in array{
      i += 1
      let label = UILabel()
      label.fram = CGRect(x: screenWidth/2 - (CGFloat(i)*50) + 25, y: 100, width: 50 , height: 20)
      label.text = text
      addSubview(label)
  }
}

在此处输入图像说明

推荐答案

您可以按照 @matt @matt 建议使用StackView.如果您不想使用StackView,则可以通过数组计数和标签索引来计算x位置

let array = ["some1", "some2", "some3"]
func setLabel(){
    var i:CGFloat = 0
    for text in array{
        i += 1
        let label = UILabel()
        label.backgroundColor = .red
        label.frame = CGRect(x: (view.bounds.width/CGFloat(array.count+1))*i-25, y: 100, width: 50 , height: 20)
        label.text = text
        view.addSubview(label)
    }
}

其他推荐答案

做到这一点的真正简单方法是将标签(一个,两个或三个)放入居中的UistackView中.您仍然需要知道标签的所需固定宽度并将宽度约束应用于每个标签,但其他所有内容都会照顾好自己.

作为演示,我使用了任意宽度为100,所有三个标签的文本都像您的图像一样.这是一个标签的外观:

在此处输入图像说明

这是三个标签的外观:

在此处输入图像说明

这是我使用的代码(sv是堆栈视图,它已经在情节提要中进行了配置; n是我们想要的数量):

    for _ in 1...n {
        let lab = UILabel()
        lab.text = "UILabel"
        lab.textAlignment = .center
        lab.translatesAutoresizingMaskIntoConstraints = false
        lab.widthAnchor.constraint(equalToConstant: 100).isActive = true
        sv.addArrangedSubview(lab)
    }

因此,您真正需要做的就是在循环时从阵列中获取正确的文本,您就完成了.关键是您根本不需要考虑框架! UistackView为我们强加了正确的帧.


注释我上面说,堆栈视图是"在情节提要中配置的",但与答案无关.您也可以在代码中创建和配置堆栈视图,而无需更改我的答案.关键是,堆栈视图已经知道如何接收任意数量的视图,将它们均匀地置于将它们置于整体上.就是这样.因此,如果您的问题所暗示,您无法管理算术来分配视图框架,为什么不让堆栈视图为您做呢?

本文地址:https://www.itbaoku.cn/post/2091157.html

问题描述

I have an array that count is between 1 to 3 varies. I want to frame them in the middle of the screen with a certain distance Also the width of the labels is fixed

let array = ["some1", "some2", "some3"]

func setLabel(){
  var i = -1
  for text in array{
      i += 1
      let label = UILabel()
      label.fram = CGRect(x: screenWidth/2 - (CGFloat(i)*50) + 25, y: 100, width: 50 , height: 20)
      label.text = text
      addSubview(label)
  }
}

enter image description here

推荐答案

You can use stackview as suggested by @matt. If you don't want to use stackview you can calculate the x position by array count and label index like this

let array = ["some1", "some2", "some3"]
func setLabel(){
    var i:CGFloat = 0
    for text in array{
        i += 1
        let label = UILabel()
        label.backgroundColor = .red
        label.frame = CGRect(x: (view.bounds.width/CGFloat(array.count+1))*i-25, y: 100, width: 50 , height: 20)
        label.text = text
        view.addSubview(label)
    }
}

其他推荐答案

The really easy way to do this is to put the labels (one, two, or three of them) into a centered UIStackView. You would still need to know the desired fixed width of a label and apply a width constraint to each label, but everything else will just take care of itself.

Just as a demonstration, I used an arbitrary width of 100, with the text for all three labels being just "UILabel" as in your image. Here's what it looks like with one label:

enter image description here

Here's what it looks like with three labels:

enter image description here

Here's the code I used (sv is the stack view, which has already been configured in the storyboard; n is how many labels we want):

    for _ in 1...n {
        let lab = UILabel()
        lab.text = "UILabel"
        lab.textAlignment = .center
        lab.translatesAutoresizingMaskIntoConstraints = false
        lab.widthAnchor.constraint(equalToConstant: 100).isActive = true
        sv.addArrangedSubview(lab)
    }

So all you really need to do is get the right text from your array as we loop, and you're done. The point is that you don't need to think about frames at all! The UIStackView imposes the correct the frames for us.


NOTE Although I said above that the stack view was "configured in the storyboard", that has nothing to do with the answer. You can create and configure the stack view in code just as well, without changing anything about my answer. The point is that the stack view already knows how to receive an arbitrary number of views, space them out evenly, and center them as a whole. That is what it is for. So if, as your question implies, you can't manage the arithmetic to assign the views a frame yourself, why not let the stack view do it for you?