0

我创建了一个纯代码应用程序,其他元素的大小被调整为不同模拟器上的测试,但是一个元素没有自动定位,我如何添加适当的约束以始终定位在设备的右岸并调整每种分辨率的大小,显然我不能动态添加约束,因为一切都是用代码完成的,它应该如何做到这一点,但是用代码。

这是我的代码

import UIKit

  class ViewController: UIViewController {

    let scrollView = UIScrollView(frame: UIScreen.mainScreen().bounds)

override func loadView() {
    // calling self.view later on will return a UIScrollView!, but we can simply call
    // self.scrollView to adjust properties of the scroll view:
    self.view = self.scrollView

    // setup the scroll view
    scrollView.backgroundColor = UIColor.whiteColor()
    // etc...
}

override func viewDidLoad() {
    super.viewDidLoad()

    var value:CGFloat = 89.0
    var total = 10
    for var index = 0; index < total; ++index {
        //View  verde
        var DynamicView=UIView(frame: CGRectMake(0, value, scrollView.frame.size.width, 198))
        DynamicView.backgroundColor=UIColor.greenColor()
        scrollView.addSubview(DynamicView)

        //Imagenn Fondo  rojo
        var fondo :UIImageView
        fondo = UIImageView(frame:CGRectMake(0, 0, scrollView.frame.size.width, 198))
        fondo.image = UIImage(named:"catedral.jpg")
        fondo.backgroundColor=UIColor.redColor()
        DynamicView.addSubview(fondo)

        //Labels Nombre y Dirección texto blanco
        var nombreLabel=UILabel(frame: CGRect(x: 8,y: 23,width: 304,height: 21))
        nombreLabel.text="Nombre Negocio"
        nombreLabel.font = UIFont(name: nombreLabel.font.fontName, size: 20)
        nombreLabel.textColor=UIColor.whiteColor()
        DynamicView.addSubview(nombreLabel)
        var direccionLabel=UILabel(frame: CGRect(x: 8,y: 44,width: 304,height: 21))
        direccionLabel.text="Dirección Negocio"
        direccionLabel.font = UIFont(name: nombreLabel.font.fontName, size: 13)
        direccionLabel.textColor=UIColor.whiteColor()
        DynamicView.addSubview(direccionLabel)

        //Puntuación  gris
        var puntuacion:UIImageView!
        puntuacion = UIImageView(frame: CGRectMake(282, 8, 30, 30))
        //puntuacion.image = UIImage(named:"7")
        puntuacion.backgroundColor=UIColor.grayColor()
        DynamicView.addSubview(puntuacion)

        //Botones  blanco
        var button1:UIButton = UIButton(frame: CGRectMake(8, 114, 32, 32))
        button1.addTarget(self, action: "button1:", forControlEvents: UIControlEvents.TouchUpInside)
        button1.backgroundColor=UIColor.whiteColor()
        DynamicView.addSubview(button1)
        button1.setImage(UIImage(named: "marker"), forState: UIControlState.Normal)

        var button2:UIButton = UIButton(frame: CGRectMake(48, 114, 32, 32))
        button2.addTarget(self, action: "button2:", forControlEvents: UIControlEvents.TouchUpInside)
        button2.backgroundColor=UIColor.whiteColor()
        DynamicView.addSubview(button2)
        button2.setImage(UIImage(named: "marker"), forState: UIControlState.Normal)

        var button3:UIButton = UIButton(frame: CGRectMake(88, 114, 32, 32))
        button3.addTarget(self, action: "button3:", forControlEvents: UIControlEvents.TouchUpInside)
        button3.backgroundColor=UIColor.whiteColor()
        DynamicView.addSubview(button3)
        button3.setImage(UIImage(named: "marker"), forState: UIControlState.Normal)

        //Cenefa Info  gris
        var cenefa :UIImageView
        cenefa = UIImageView(frame:CGRectMake(0, 150, scrollView.frame.size.width, 48))
        cenefa.backgroundColor=UIColor.grayColor()
        cenefa.alpha=1
        DynamicView.addSubview(cenefa)

        //Info Ccenefa  amarillo
        var descripcion=UILabel(frame: CGRect(x: 2,y: 0,width: scrollView.frame.size.width, height: 45))
        descripcion.backgroundColor=UIColor.yellowColor()
        descripcion.text="Descripcion Negocio"
        descripcion.font = UIFont(name: nombreLabel.font.fontName, size: 10)
        descripcion.textColor=UIColor.blueColor()
        cenefa.addSubview(descripcion)

        value=value+200.0

    }
    scrollView.contentSize=CGSizeMake(scrollView.frame.size.width, 2089)

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

这段代码放置了一个灰色方块,但在 Iphone 6 中没有正确对齐,加上这是代码

//Cenefa Info  gris
        var cenefa :UIImageView
        cenefa = UIImageView(frame:CGRectMake(0, 150, scrollView.frame.size.width, 48))
        cenefa.backgroundColor=UIColor.grayColor()
        cenefa.alpha=1
        DynamicView.addSubview(cenefa)
4

1 回答 1

0

您可以在代码中尝试:

UIDevice.currentDevice().userInterfaceIdiom == .Pad ? new position : original position

您可以将 .Phone 用于 iPhone。

在此示例中,您将使用原始位置为 iPhone 定位元素,然后仅为 iPad 设置新位置。非常适合使用 CGPoints,这是我如何使用它的示例:

image.position = CGPoint(x: size.width * 0.25, y: size.height * 0.65) 
image.position = UIDevice.currentDevice() .UserInterfaceIdiom == .Pad ? CGPoint(size.width * 0.3, y: size.height * 0.7) : CGPoint(x: size.width * 0.25, y: size.height * 0.65)
于 2015-09-10T19:19:53.720 回答