最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]swift画登录界面视图

Swift crifan 2871浏览 0评论

想要用swift去画登录页面

 

搜:

swift login register
swift login example
swift login demo
参考:
搜:
swift login demo without storyboard
swift login  no storyboard
参考:
然后用自己写代码:
AppDelegate.swift
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
 。。。
        let loginVC = LoginViewController()
        self.window?.rootViewController = loginVC

        self.window!.makeKeyAndVisible()

        return true
    }
LoginViewController.swift
//
//  LoginViewController.swift
//  JianDao
//
//  Created by licrifan on 15/11/18.
//  Copyright © 2015年 licrifan. All rights reserved.
//

import UIKit

class LoginViewController: UIViewController {
    var appLogoImage:UIImage = UIImage(named: "app_logo_daryun")!
    var appLogoImageView:UIImageView = UIImageView()
    var appNameLabel:UILabel = UILabel()
    var usernameTextField:UITextField = UITextField()
    var passwordTextField:UITextField = UITextField()
    var loginButton:UIButton = UIButton()

    override func viewDidLoad() {
        self.view.backgroundColor = UIColor.whiteColor()


        let screenCenterXPos = self.view.bounds.width/2

        //1. app logo image
        appLogoImageView.image = appLogoImage
        appLogoImageView.frame = CGRectMake(
            screenCenterXPos - appLogoImage.size.width/2,
            self.view.bounds.height*0.15,
            appLogoImage.size.width,
            appLogoImage.size.height)
        //print("appLogoImageView.frame=\(appLogoImageView.frame)")
        //appLogoImageView.frame=(78.5, 66.7, 218.0, 142.0)
        //appLogoImageView.frame=(133.0, 66.7, 109.0, 71.0)
        //appLogoImageView.center.x = screenCenterXPos

        //2. app name label
        appNameLabel.frame = CGRectMake(
            0,
            appLogoImageView.frame.origin.y + appLogoImageView.frame.height + 20,//self.view.bounds.height*0.25,
            200,
            40)
        appNameLabel.text = "简道"
        //appNameLabel.font = UIFont.boldSystemFontOfSize(22)
        appNameLabel.font = UIFont.systemFontOfSize(22)
        //print("appNameLabel.frame=\(appNameLabel.frame)") //appNameLabel.frame=(0.0, 166.75, 200.0, 40.0)
        appNameLabel.sizeToFit()
        //print("appNameLabel.frame=\(appNameLabel.frame)") //appNameLabel.frame=(0.0, 166.75, 36.0, 21.5)
        appNameLabel.center.x = screenCenterXPos
        //print("appNameLabel.frame=\(appNameLabel.frame)") //appNameLabel.frame=(169.5, 166.75, 36.0, 21.5)

        //3. username/phone number textFiled
        let textFieldPaddingX:CGFloat = 10
        let textFieldPaddingY:CGFloat = 20
        let textFieldHeight:CGFloat = 40
        usernameTextField.frame = CGRectMake(
            textFieldPaddingX,
            appNameLabel.frame.origin.y + appNameLabel.frame.height + 40,//self.view.bounds.height*0.4,
            self.view.bounds.width - (textFieldPaddingX * 2),
            textFieldHeight)
        usernameTextField.textAlignment = NSTextAlignment.Left
        usernameTextField.font = UIFont.systemFontOfSize(16)
        usernameTextField.borderStyle = UITextBorderStyle.RoundedRect
//        usernameTextField.layer.borderWidth = 1
//        usernameTextField.layer.cornerRadius = 4
//        usernameTextField.layer.borderColor = UIColor.lightGrayColor().CGColor

        //4. password textFiled
        passwordTextField.frame = CGRectMake(
            usernameTextField.frame.origin.x,
            usernameTextField.frame.origin.y + usernameTextField.frame.height + textFieldPaddingY,
            usernameTextField.frame.width,
            usernameTextField.frame.height)
        passwordTextField.textAlignment = NSTextAlignment.Left
        passwordTextField.font = UIFont.systemFontOfSize(14)
        passwordTextField.borderStyle = UITextBorderStyle.RoundedRect

        //5. login button
        loginButton.frame = CGRectMake(
            0,
            passwordTextField.frame.origin.y
                + passwordTextField.frame.height
                + textFieldPaddingY*2,
            passwordTextField.frame.width - 40,
            usernameTextField.frame.height)
        loginButton.center.x = screenCenterXPos
        loginButton.backgroundColor = UIColor(hexString: "#249ed8")
        loginButton.tintColor = UIColor.whiteColor()
        loginButton.titleLabel?.font = UIFont.systemFontOfSize(16)
        loginButton.setTitle("登录", forState: UIControlState.Normal)
        loginButton.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        loginButton.addTarget(self, action: Selector("loginAction"), forControlEvents: UIControlEvents.TouchUpInside)
        loginButton.layer.cornerRadius = 16


        //for debug
        usernameTextField.text = "[email protected]"
        passwordTextField.text = "daryun123"

        self.view.addSubview(appLogoImageView)
        self.view.addSubview(appNameLabel)
        self.view.addSubview(usernameTextField)
        self.view.addSubview(passwordTextField)
        self.view.addSubview(loginButton)
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    func loginAction() {
        print("loginAction")

        let mainViewVC = MainViewController()
        let mainNaviVC = UINavigationController(rootViewController:mainViewVC)

        self.showViewController(mainNaviVC, sender: self)
    }
}
效果是:
 swift login ui username and password

转载请注明:在路上 » [已解决]swift画登录界面视图

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
85 queries in 0.173 seconds, using 22.06MB memory