swift界面設計例子




import UIKit

class ViewController: UIViewController,UITableViewDataSource {
var submitButton : UIButton!
var nameLabel : UILabel!
var passwordLabel : UILabel!
var userText : UITextField!
var passwordText : UITextField!
var loginDateText : UITextField!
var loginDatePicker:UIDatePicker!
var backImage:UIImageView!
var tableView:UITableView!
var items :NSMutableArray?
override func viewDidLoad() {
println("load Controller")
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

//label define
nameLabel=UILabel()
nameLabel.frame=CGRectMake(0, 20, 100, 30)
nameLabel.text="用戶名"
nameLabel.textAlignment=NSTextAlignment.Center
nameLabel.textColor=UIColor.greenColor()

userText=UITextField()
userText.frame=CGRectMake(110, 20, 100, 30)
userText.text="用戶名"
userText.textAlignment=NSTextAlignment.Center
userText.textColor=UIColor.grayColor()
userText.borderStyle=UITextBorderStyle.Bezel

passwordLabel=UILabel()
passwordLabel.frame=CGRectMake(0, 60, 100, 30)
passwordLabel.text="密碼"
passwordLabel.textAlignment=NSTextAlignment.Center
passwordLabel.textColor=UIColor.greenColor()


passwordText=UITextField()
passwordText.frame=CGRectMake(110, 60, 100, 30)
passwordText.text="密碼"
passwordText.textAlignment=NSTextAlignment.Center
passwordText.textColor=UIColor.grayColor()
passwordText.secureTextEntry=true//密碼
passwordText.borderStyle=UITextBorderStyle.Line//邊框

loginDatePicker=UIDatePicker()

loginDatePicker.datePickerMode=UIDatePickerMode.Date
loginDatePicker.locale = NSLocale(localeIdentifier: "zh_CN")


loginDateText=UITextField()
loginDateText.frame=CGRectMake(0, 100, 100, 30)
loginDateText.text=loginDatePicker.date.description
loginDateText.textAlignment=NSTextAlignment.Center




loginDateText.inputView=loginDatePicker

backImage=UIImageView(frame:self.view.bounds)
backImage.image=UIImage(named:"mainBack")

//button define
submitButton=UIButton() //實例化
submitButton.frame=CGRectMake(0,140,200,30)//大小、位置
submitButton.backgroundColor=UIColor.redColor()//背景色
submitButton.setTitle("登錄",forState:UIControlState.Normal)//文本
submitButton.addTarget(self,action:"submitInfo:",forControlEvents:UIControlEvents.TouchUpInside)//事件

var nsd = NSData(contentsOfURL:NSURL.URLWithString("http://ww2.sinaimg.cn/bmiddle/632dab64jw1ehgcjf2rd5j20ak07w767.jpg"))
var img = UIImage(data: nsd);
var vImg = UIImageView(image: img);
// vImg.frame.origin = CGPoint(x:0,y:220);
vImg.frame = CGRect(x:0,y:220,width:60,height:60);
submitButton.setBackgroundImage(img, forState: UIControlState.Normal)


tableView=UITableView()
tableView.frame=CGRectMake(0, 200, 400, 400)
tableView.dataSource=self

self.items = NSMutableArray()
self.items?.addObject("abc")
self.items?.addObject("hhh")
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
self.view.addSubview(backImage)
self.view.addSubview(loginDateText)
self.view.addSubview(passwordLabel)
self.view.addSubview(passwordText)
self.view.addSubview(userText)
self.view.addSubview(nameLabel)
self.view.addSubview(submitButton)

self.view.addSubview(vImg);
self.view.addSubview(tableView);

}

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

func submitInfo(button: UIView) {
let alert = UIAlertView()
alert.title = "計時完成!"
alert.message = userText.text+passwordText.text
alert.addButtonWithTitle("OK")
alert.show()
}
func tableView(tableView: UITableView!, titleForFooterInSection section: Int) -> String! {
return "foot"
}
func tableView(tableView: UITableView!, titleForHeaderInSection section: Int) -> String! {
return "header"
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
let cell = tableView .dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
cell.textLabel.text = self.items?.objectAtIndex(indexPath.row).stringValue
return cell
}
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int
{
return self.items!.count
}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章