2015-12-14 76 views
2

我無法讓文字留在屏幕寬度內。我縮小了文字,不想讓它有任何的縮小(請參見下文):用戶界面通過故事板或以編程方式?

enter image description here

它會更好,在具有自適應佈局,適合在屏幕方面,而是要使用故事板以編程方式執行它?

這是我現有的左視圖控制器代碼:

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! 
    } 
} 

感謝您的任何建議!

+0

目前有些不清楚(視覺上)您現在擁有什麼樣的結果,但作爲一般規則 - 嘗試使用「故事板」 '儘可能多 - **「少代碼=少bug」** –

+0

謝謝。我的下一個問題是,我將如何將我目前的代碼轉換爲Storyboard佈局? – mhs5819

回答

1

基於故事板的方法處理許多必要的細節以支持自適應UI。

除此之外,你可以重新考慮你的設計。這不是iOS的本地設計,因爲iOS沒有單選按鈕或水平壓縮的拆分表格視圖。

您可以查看現有的單位換算應用,以瞭解他們如何讓用戶選擇單元。

一個建議是隻要求一個單位(一次)。一旦你知道一個單位,你可以預測第二個單位,以保存用戶不必選擇可能的選擇。無論哪種方式,您都應該考慮特質類以及您的UI如何適應不同設備(例如,模式,彈出窗口,分割視圖)。

至於用戶界面,你可以通過一個選擇器,一個tableview的複選標記附件視圖,或一個自定義控件。只要記住用戶對用戶界面越熟悉和直觀,用戶就越不可能被如何使用你的應用程序所困惑。

您呈現的選擇越少,用戶就越好。例如,如果有人位於英國,他們可能不需要轉換爲美國的度量。如果您絕對需要提供所有可能的選擇,請考慮將度量分成不同的部分(按國家或系統)。

+0

感謝您提出這些建議。我已經找到了一個關於使用兩個並排選擇器視圖進行單位轉換的教程,比使用代碼簡單得多。 – mhs5819

1

看起來你已經在桌面單元格&中設計了UI,你正在以編程方式設置它的高度。你可以在屬性檢查器中將界面生成器中的標籤的lines屬性設置爲適當的行,以便相應地調整你的文本。確保從尺寸檢查器中增加桌面視圖行的大小,以便您的單元格相應地適合

相關問題