V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
maiziedu
V2EX  ›  Swift

[分享] 使用 swift 实现自适应屏幕代码片段

  •  
  •   maiziedu · 2015-12-16 17:37:05 +08:00 · 3053 次点击
    这是一个创建于 3046 天前的主题,其中的信息可能已经有所发展或是发生改变。

    本月初,苹果宣布 swift 编程语言开源,最近苹果软件工程高级副总裁 Craig Federighi 又坦言, Swift 将深度整合到 iCloud 和 OS X 中,这意味着 swift 将成为今后 iOS 开发的主流语言,从事 iOS 开发, swift 是必学语言。

    看到苹果近期的消息有点鸡冻,废话不多说,今天要和大家分享的是在网上看到的关于使用 swift 语言纯代码实现 iphone 软件自适应屏幕的源码,一起来看看吧。

    //
    // ViewController.swift
    // kids
    //
    // Created by yuexiaosheng 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.baidu.com")
        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.
    }
    

    }

    自适应屏幕源码分享到此,强烈建议大家可以自己动手跑一下上述代码。

    相关文章:《 Swift 2.0 中类型检查与转换详解》 http://www.maiziedu.com/group/article/10383/

    文章来源:月小升博客

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5268 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 06:01 · PVG 14:01 · LAX 23:01 · JFK 02:01
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.