我将有一个UIButton对象的正方形网格。每个UIButton都引用了唯一的选项和ID(来自数组API)。
选项API的示例
options = (
{
name = Color;
"option_id" = 16;
"product_option_id" = 1324;
"product_option_value" = (
{
image = "example.com/image.jpg";
name = red;
"option_value_id" = 104;
price = 0;
"price_prefix" = "+";
"product_option_value_id" = 4650;
},
{
image = "https://www.ishtari.com/image/cache/data/categories/color/Blue-1125.000000x1125.000000_0.jpg";
name = blue;
"option_value_id" = 91;
price = 0;
"price_prefix" = "+";
"product_option_value_id" = 4649;
}
);
我正在阅读API并在UIButtons中列出该数组
else if dict["type"].stringValue == "image" {
let imageOptionView = UIView(frame: CGRect(x: 5, y: Y, width: self.optionView.frame.size.width - 10, height: 30))
imageOptionView.isUserInteractionEnabled = true
self.optionView.addSubview(imageOptionView)
imageOptionView.tag = i
self.imageId.append("")
Y += 30;
let productOptionArray : JSON = JSON(dict["product_option_value"].arrayObject!)
let imagesArray:NSMutableArray = []
var internalY:CGFloat = 0
for j in 0..<productOptionArray.count {
let img = RadioButton(frame: CGRect(x: 5, y: internalY, width: 30, height: 30))
let imgName = productOptionArray[j]["name"].stringValue
img.setTitle(imgName, for: .normal)
img.setTitleColor(UIColor.black, for: .normal)
img.titleLabel?.font = UIFont.init(name: REGULARFONT, size: 13.0)
let images = productOptionArray[j]["image"].stringValue
let data = NSData(contentsOf: NSURL(string: images)! as URL)
img.setImage(UIImage(data: data! as Data), for: .normal)
img.titleEdgeInsets = UIEdgeInsetsMake(0, 6, 0, 0)
img.addTarget(self, action: #selector(self.onRadioButtonValueChangedAccount), for:.touchUpInside)
img.tag = j
imageOptionView.addSubview(img)
internalY += 40;
imagesArray.add(img)
}
var paymentInformationContainerFrame = imageOptionView.frame
paymentInformationContainerFrame.size.height = internalY
imageOptionView.frame = paymentInformationContainerFrame
let imgButton:RadioButton = RadioButton()
//imgButton.groupButtons = imagesArray as! [RadioButton]
Y += internalY;
但是我希望在不使用CollectionView的情况下以编程方式在网格视图中列出它们,这怎么可能?
您可以看到我基于UIButton类定义了RadioButton类。我正在使用Swift 3
编辑:在论坛的此处找到Objective-C的解决方案,然后将其转换为Swift 3 HERE
else if dict["type"].stringValue == "image" {
let imageOptionView = UIView(frame: CGRect(x: 5, y: Y, width: self.optionView.frame.size.width - 10, height: 30))
imageOptionView.isUserInteractionEnabled = true
self.optionView.addSubview(imageOptionView)
imageOptionView.tag = i
self.imageId.append("")
Y += 30;
let productOptionArray : JSON = JSON(dict["product_option_value"].arrayObject!)
let imagesArray:NSMutableArray = []
var internalY:CGFloat = 0
for j in 0..<productOptionArray.count {
var rows: Int = 5
var columns: Int = 1
for n in 0..<rows {
for m in 0..<columns {
var frame = CGRect(x: 1 + 45 * n, y: Int(internalY) * m, width: 40, height: 40)
var img = RadioButton(frame: frame) as? RadioButton
let imgName = productOptionArray[j]["name"].stringValue
img?.setTitle(imgName, for: .normal)
img?.setTitleColor(UIColor.black, for: .normal)
img?.titleLabel?.font = UIFont.init(name: REGULARFONT, size: 13.0)
let images = productOptionArray[j]["image"].stringValue
let data = NSData(contentsOf: NSURL(string: images)! as URL)
img?.setImage(UIImage(data: data! as Data), for: .normal)
img?.titleEdgeInsets = UIEdgeInsetsMake(0, 6, 0, 0)
img?.addTarget(self, action: #selector(self.onRadioButtonValueChangedAccount), for:.touchUpInside)
img?.tag = j + n * columns + m
imageOptionView.addSubview(img!)
internalY += 40;
imagesArray.add(img!)
print("images", images)
print("imagesArray", imagesArray)
}
}
}
var paymentInformationContainerFrame = imageOptionView.frame
paymentInformationContainerFrame.size.height = internalY
imageOptionView.frame = paymentInformationContainerFrame
let imgButton:RadioButton = RadioButton()
imgButton.groupButtons = imagesArray as! [RadioButton]
Y += internalY;
}
现在我在“网格视图”中获取了按钮,但是我没有获取正确的值。例如,不是显示3个视图(对象),而是将1个视图(对象)重复5次,而其他视图没有显示。加上此视图和下一个视图之间的水平间隔(Y),尽管我多次更改了Y的值但不起作用。
imageArray和图像的打印结果
images https://example.com/option-1.jpg
imagesArray (
"<RadioButton: 0x7fdfafce1b00; baseClass = UIButton; frame = (1 0; 40 40); opaque = NO; layer = <CALayer: 0x60400062c660>>",
"<RadioButton: 0x7fdfafce2e30; baseClass = UIButton; frame = (46 0; 40 40); opaque = NO; layer = <CALayer: 0x600000629700>>",
"<RadioButton: 0x7fdfafdc1d00; baseClass = UIButton; frame = (91 0; 40 40); opaque = NO; layer = <CALayer: 0x604000629e20>>",
"<RadioButton: 0x7fdfafd99910; baseClass = UIButton; frame = (136 0; 40 40); opaque = NO; layer = <CALayer: 0x60400062e2a0>>",
"<RadioButton: 0x7fdfafcdc960; baseClass = UIButton; frame = (181 0; 40 40); opaque = NO; layer = <CALayer: 0x60000062ff40>>"
)
images https://example.com/option-2.jpg
imagesArray (
"<RadioButton: 0x7fdfafce1b00; baseClass = UIButton; frame = (1 0; 40 40); opaque = NO; layer = <CALayer: 0x60400062c660>>",
"<RadioButton: 0x7fdfafce2e30; baseClass = UIButton; frame = (46 0; 40 40); opaque = NO; layer = <CALayer: 0x600000629700>>",
"<RadioButton: 0x7fdfafdc1d00; baseClass = UIButton; frame = (91 0; 40 40); opaque = NO; layer = <CALayer: 0x604000629e20>>",
"<RadioButton: 0x7fdfafd99910; baseClass = UIButton; frame = (136 0; 40 40); opaque = NO; layer = <CALayer: 0x60400062e2a0>>",
"<RadioButton: 0x7fdfafcdc960; baseClass = UIButton; frame = (181 0; 40 40); opaque = NO; layer = <CALayer: 0x60000062ff40>>",
"<RadioButton: 0x7fdfafdccc30; baseClass = UIButton; frame = (1 0; 40 40); opaque = NO; tag = 1; layer = <CALayer: 0x60400063e8a0>>"
)
images https://example.com/option-2.jpg
imagesArray (
"<RadioButton: 0x7fdfafce1b00; baseClass = UIButton; frame = (1 0; 40 40); opaque = NO; layer = <CALayer: 0x60400062c660>>",
"<RadioButton: 0x7fdfafce2e30; baseClass = UIButton; frame = (46 0; 40 40); opaque = NO; layer = <CALayer: 0x600000629700>>",
"<RadioButton: 0x7fdfafdc1d00; baseClass = UIButton; frame = (91 0; 40 40); opaque = NO; layer = <CALayer: 0x604000629e20>>",
"<RadioButton: 0x7fdfafd99910; baseClass = UIButton; frame = (136 0; 40 40); opaque = NO; layer = <CALayer: 0x60400062e2a0>>",
"<RadioButton: 0x7fdfafcdc960; baseClass = UIButton; frame = (181 0; 40 40); opaque = NO; layer = <CALayer: 0x60000062ff40>>",
"<RadioButton: 0x7fdfafdccc30; baseClass = UIButton; frame = (1 0; 40 40); opaque = NO; tag = 1; layer = <CALayer: 0x60400063e8a0>>",
"<RadioButton: 0x7fdfafde4430; baseClass = UIButton; frame = (46 0; 40 40); opaque = NO; tag = 1; layer = <CALayer: 0x60400063bd00>>"
)
images https://example.com/option-2.jpg
imagesArray (
"<RadioButton: 0x7fdfafce1b00; baseClass = UIButton; frame = (1 0; 40 40); opaque = NO; layer = <CALayer: 0x60400062c660>>",
"<RadioButton: 0x7fdfafce2e30; baseClass = UIButton; frame = (46 0; 40 40); opaque = NO; layer = <CALayer: 0x600000629700>>",
"<RadioButton: 0x7fdfafdc1d00; baseClass = UIButton; frame = (91 0; 40 40); opaque = NO; layer = <CALayer: 0x604000629e20>>",
"<RadioButton: 0x7fdfafd99910; baseClass = UIButton; frame = (136 0; 40 40); opaque = NO; layer = <CALayer: 0x60400062e2a0>>",
"<RadioButton: 0x7fdfafcdc960; baseClass = UIButton; frame = (181 0; 40 40); opaque = NO; layer = <CALayer: 0x60000062ff40>>",
"<RadioButton: 0x7fdfafdccc30; baseClass = UIButton; frame = (1 0; 40 40); opaque = NO; tag = 1; layer = <CALayer: 0x60400063e8a0>>",
"<RadioButton: 0x7fdfafde4430; baseClass = UIButton; frame = (46 0; 40 40); opaque = NO; tag = 1; layer = <CALayer: 0x60400063bd00>>",
"<RadioButton: 0x7fdfafde6200; baseClass = UIButton; frame = (91 0; 40 40); opaque = NO; tag = 1; layer = <CALayer: 0x60400062b620>>"
)
images https://example.com/option-2.jpg
imagesArray (
"<RadioButton: 0x7fdfafce1b00; baseClass = UIButton; frame = (1 0; 40 40); opaque = NO; layer = <CALayer: 0x60400062c660>>",
"<RadioButton: 0x7fdfafce2e30; baseClass = UIButton; frame = (46 0; 40 40); opaque = NO; layer = <CALayer: 0x600000629700>>",
"<RadioButton: 0x7fdfafdc1d00; baseClass = UIButton; frame = (91 0; 40 40); opaque = NO; layer = <CALayer: 0x604000629e20>>",
"<RadioButton: 0x7fdfafd99910; baseClass = UIButton; frame = (136 0; 40 40); opaque = NO; layer = <CALayer: 0x60400062e2a0>>",
"<RadioButton: 0x7fdfafcdc960; baseClass = UIButton; frame = (181 0; 40 40); opaque = NO; layer = <CALayer: 0x60000062ff40>>",
"<RadioButton: 0x7fdfafdccc30; baseClass = UIButton; frame = (1 0; 40 40); opaque = NO; tag = 1; layer = <CALayer: 0x60400063e8a0>>",
"<RadioButton: 0x7fdfafde4430; baseClass = UIButton; frame = (46 0; 40 40); opaque = NO; tag = 1; layer = <CALayer: 0x60400063bd00>>",
"<RadioButton: 0x7fdfafde6200; baseClass = UIButton; frame = (91 0; 40 40); opaque = NO; tag = 1; layer = <CALayer: 0x60400062b620>>",
"<RadioButton: 0x7fdfafde8320; baseClass = UIButton; frame = (136 0; 40 40); opaque = NO; tag = 1; layer = <CALayer: 0x60400063c400>>"
)
images https://example.com/option-2.jpg
imagesArray (
"<RadioButton: 0x7fdfafce1b00; baseClass = UIButton; frame = (1 0; 40 40); opaque = NO; layer = <CALayer: 0x60400062c660>>",
"<RadioButton: 0x7fdfafce2e30; baseClass = UIButton; frame = (46 0; 40 40); opaque = NO; layer = <CALayer: 0x600000629700>>",
"<RadioButton: 0x7fdfafdc1d00; baseClass = UIButton; frame = (91 0; 40 40); opaque = NO; layer = <CALayer: 0x604000629e20>>",
"<RadioButton: 0x7fdfafd99910; baseClass = UIButton; frame = (136 0; 40 40); opaque = NO; layer = <CALayer: 0x60400062e2a0>>",
"<RadioButton: 0x7fdfafcdc960; baseClass = UIButton; frame = (181 0; 40 40); opaque = NO; layer = <CALayer: 0x60000062ff40>>",
"<RadioButton: 0x7fdfafdccc30; baseClass = UIButton; frame = (1 0; 40 40); opaque = NO; tag = 1; layer = <CALayer: 0x60400063e8a0>>",
"<RadioButton: 0x7fdfafde4430; baseClass = UIButton; frame = (46 0; 40 40); opaque = NO; tag = 1; layer = <CALayer: 0x60400063bd00>>",
"<RadioButton: 0x7fdfafde6200; baseClass = UIButton; frame = (91 0; 40 40); opaque = NO; tag = 1; layer = <CALayer: 0x60400062b620>>",
"<RadioButton: 0x7fdfafde8320; baseClass = UIButton; frame = (136 0; 40 40); opaque = NO; tag = 1; layer = <CALayer: 0x60400063c400>>",
"<RadioButton: 0x7fdfafdea760; baseClass = UIButton; frame = (181 0; 40 40); opaque = NO; tag = 1; layer = <CALayer: 0x60400062f760>>"
)
答案 0 :(得分:1)
如果您不想使用集合视图,则可以使用包含多个水平堆栈视图的垂直堆栈视图,或者可以使用滚动视图(如果希望它们可滚动)或包含UIView和自定义布局代码。
请注意,可以使用约束将固定数量的视图均匀分布,这些约束将每个视图的中心位置设置为其容器视图的宽度/高度的一部分
对于x位置,您可以将水平中心约束设置为:
center = (width / (number_of_views + 1) * (view_index + 1)
因此,如果有3个视图,则第一个视图的水平中心(视图0)将为:
center = (width/4) * (0 + 1)
或
center = width/4
因此,您将使最左边的视图的中心位置相对于父视图的宽度乘以0.25。将相同的公式应用于x和y尺寸的网格中的所有视图。