Search code examples
iosxcodeswiftconstraintsresolutions

How change the position one element for different resolutions in swift


I created an application to pure code other elements are resized as do tests on different simulators , but one element is not positioned automatically , how I add the appropriate constraints to be always positioned on the right bank of the devices and resize each type of resolution , obviously I can not add the constraints dynamically as everything was done with code , how it should do this but with code.

This is my code

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.
}

} This code place a gray square but this not alignment correctly in Iphone 6 and plus this is the code

//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)

Solution

  • You can try this in code:

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

    You can use .Phone for the iPhone.

    In this example, you'd use the original position to position the element for the iPhone, then the new position would only be set for the iPad. Perfect for using CGPoints, here is an example of how I've used it:

    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)