我無法讓文字留在屏幕寬度內。我縮小了文字,不想讓它有任何的縮小(請參見下文):用戶界面通過故事板或以編程方式?
它會更好,在具有自適應佈局,適合在屏幕方面,而是要使用故事板以編程方式執行它?
這是我現有的左視圖控制器代碼:
import UIKit
protocol LeftViewControllerDelegate {
func leftUnitSelected(index: WUnit)
func leftDataChanged(text: String)
}
class LeftViewController: UIViewController,UITableViewDelegate,UITextFieldDelegate {
@IBOutlet weak var leftTextField: UITextField!
var dataArray: [String] = []
var delegate: LeftViewControllerDelegate?
override func viewDidLoad() {
super.viewDidLoad()
self.dataArray = ["Litre", "MilliLitre", "Fluid Ounce (US)", "Fluid Ounce (UK)", "Teaspoon (US)", "Teaspoon (UK)", "Tablespoon (US)", "Tablespoon (UK)", "Cup (US)", "Cup (UK)", "Pint (US)", "Pint (UK)", "Quart (US)", "Quart (UK)", "Gallon (US)", "Gallon (UK)"]
NSNotificationCenter.defaultCenter().addObserver(self, selector: "textFieldTextDidChangeOneCI:", name: UITextFieldTextDidChangeNotification, object: self.leftTextField)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//MARK: UITableView Datasource
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.dataArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
cell.textLabel!.text = self.dataArray[indexPath.row]
cell.textLabel!.font = UIFont(name: cell.textLabel!.font.fontName, size:12) // Change the font size as per your requirement
cell.imageView!.image = UIImage(named: "20x20_empty-round-radiobutton-choice-ui")
return cell
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 32
}
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
let cell: UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
cell.imageView!.image = UIImage(named: "20x20_empty-round-radiobutton-choice-ui")
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell: UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
cell.imageView!.image = UIImage(named: "20x20_round-choice-radiobutton-ui")
if let delegate = delegate {
delegate.leftUnitSelected(WUnit(rawValue: indexPath.row)!)
}
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String {
return "From"
}
//MARK:
func setText(text: String) {
self.leftTextField.text = text
}
func textFieldTextDidChangeOneCI(notification: NSNotification) {
let textfield: UITextField = notification.object! as! UITextField
if Double(textfield.text!) > 0.0 {
if let delegate = delegate {
delegate.leftDataChanged(textfield.text!)
}
}
}
func text() -> String {
return self.leftTextField.text!
}
}
感謝您的任何建議!
目前有些不清楚(視覺上)您現在擁有什麼樣的結果,但作爲一般規則 - 嘗試使用「故事板」 '儘可能多 - **「少代碼=少bug」** –
謝謝。我的下一個問題是,我將如何將我目前的代碼轉換爲Storyboard佈局? – mhs5819