我无法让文字留在屏幕宽度内。我缩小了文字,不想让它有任何的缩小(请参见下文):用户界面通过故事板或以编程方式?
它会更好,在具有自适应布局,适合在屏幕方面,而是要使用故事板以编程方式执行它?
这是我现有的左视图控制器代码:
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