ios開發之swift 使用代碼來自適應介面

來源:互聯網
上載者:User

月小升曾經使用介面來進行自適應,作為沒有經過系統化xcode訓練的人來說,太麻煩了。還是代碼來得直接。

直接計算螢幕的寬度,就可以設定元件的大小了。

 代碼如下 複製代碼

//
//  ViewController.swift
//  kids
//
//  Created by  on 15/9/4.
//  Copyright (c) 2015年 Acrowdkids. All rights reserved.
//
 
import UIKit
 
class ViewController: UIViewController {
 
    @IBOutlet weak var wb: UIWebView!
    @IBOutlet weak var bn_blue: UIButton!
 
    @IBOutlet weak var bn_org: UIButton!
 
    @IBOutlet weak var lb_msg: UILabel!
    //var webView : UIWebView?
    override func viewDidLoad() {
        super.viewDidLoad()
 
        // Do any additional setup after loading the view, typically from a nib.
        lb_msg.text = self.view.bounds.width.description
        var width = self.view.bounds.width.description
        var height = self.view.bounds.height.description
        var fwidth = (width as NSString).floatValue
        var fheight = (height as NSString).floatValue//螢幕高度
 
        let sizew:CGFloat = CGFloat(fwidth/2)
        let posy:CGFloat = CGFloat(fheight-40)
 
        let sizewebview:CGFloat = CGFloat(fwidth)
        let posywebview:CGFloat = CGFloat(fheight-60)
 
 
        let registerBtn : UIButton = UIButton()
        registerBtn.frame = CGRectMake(0, posy, sizew, 40)
        registerBtn.backgroundColor = UIColor.orangeColor()
        registerBtn.setTitle("註冊", forState: UIControlState.Normal)
        registerBtn.addTarget(self, action: "goList:", forControlEvents:UIControlEvents.TouchUpInside)
        self.view.addSubview(registerBtn)
 
        let registerBtn2 : UIButton = UIButton()
        registerBtn2.frame = CGRectMake(sizew, posy, sizew, 40)
        registerBtn2.backgroundColor = UIColor.blueColor()
        registerBtn2.setTitle("登陸", forState: UIControlState.Normal)
        registerBtn2.addTarget(self, action: "goList:", forControlEvents:UIControlEvents.TouchUpInside)
        self.view.addSubview(registerBtn2)
 
        var webView = UIWebView()
        var url = NSURL(string:http://www.111cn.net)
        var urlRequest = NSURLRequest(URL:url!)
        webView.frame = CGRectMake(0, 20, sizewebview, posywebview)
        webView.loadRequest(urlRequest)
        self.view.addSubview(webView)
 
 
    }
 
    @IBAction func goList(sender: AnyObject) {
        var rootVC :ListViewController = ListViewController(nibName: "ListViewController", bundle: nil)
 
        let NVC :UINavigationController = UINavigationController(rootViewController: rootVC)
        NVC.setNavigationBarHidden(true, animated: true)
 
        self.presentViewController(NVC, animated: true, completion: nil)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
 
 
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.