0
我嘗試使用兩行中的3個按鈕創建按鈕面板。由於它並不需要靈活,我想iOS以編程方式在中心添加圖像視圖
class MyViewController : UIViewController {
@IBOutlet weak var buttonPanel: UIView! // auto layout, aligned to the bottom of screen
var icon0: UIImageView!
var icon1: UIImageView!
...
var icon5: UIImageView!
override func viewDidLoad() {
super.viewDidLoad();
icon0 = UIImageView(image: UIImage(named: "ic_smiley_gray.png"))
icon1 = UIImageView(image: UIImage(named: "ic_smiley_gray.png"))
// ...
icon5 = UIImageView(image: UIImage(named: "ic_smiley_gray.png"))
let icons = [icon0, icon1, ..., icon5]
let btnCellW = CGFloat(self.view.bounds.width)/3.0
let btnCellH = buttonPanel.frame.height/2.0
for j in 0..<2 {
for i in 0..<3 {
let rect = CGRect(x: btnCellW * CGFloat(i),
y: btnCellH * CGFloat(j),
width: btnCellW, height: btnCellH)
let cell = UIView(frame: rect)
cell.layer.borderWidth = 0.5
cell.layer.borderColor = UIColor.grayColor().CGColor
cell.layer.backgroundColor = UIColor(red: 0.85 + 0.15 * CGFloat(1 - i % 2), green: 0.85, blue: 0.85,
alpha: 1.0).CGColor
buttonPanel.addSubview(cell)
let ic = icons[i + j*3]
ic.center = cell.center
ic.layer.borderWidth = 0.5
ic.layer.borderColor = UIColor.brownColor().CGColor
NSLog("adding icon \(ic.image) at \(ic.center.x) x \(ic.center.y)")
cell.addSubview(ic)
}
}
按鈕面板與細胞正確繪製,但只有第一個單元具有的圖像,所以圖像的資產是不錯的。其他單元格是空白的。我的問題是什麼?
你是對的。我錯誤地認爲中心在屏幕座標中。 –