顯示具有 Swift 標籤的文章。 顯示所有文章
顯示具有 Swift 標籤的文章。 顯示所有文章

2015年3月11日 星期三

[Xcode] 警告: Detected a case where constraints ambiguously suggest a height of zero for a tableview cell’s content view.

在使用UITableView時,如果有自訂儲存格的高度,要記得另外要設定好rowHeight屬性喔!不然會碰到警告訊息:

Warning once only: Detected a case where constraints ambiguously suggest a height of zero for a tableview cell’s content view. We’re considering the collapse unintentional and using standard height instead.

我這次是載入自訂的儲存格XIB,也有另外設定了rowHeight,忘了妥善處理,所以收到了這個警告,處理的方法很簡單~只要加上這兩個方法~在紅字的地方填上你要的儲存格高度(CGFloat),即可:
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 你要的儲存格高度
    }
    
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 你要的儲存格高度
    }

又或者更簡便的方法是直接在viewDidLoad中加上下述程式碼,即可:
self.tableView.rowHeight = 36.0

參考資料:[Stack Overflow] iOS8 - constraints ambiguously suggest a height of zero: link

2015年3月2日 星期一

[Xcode] 錯誤:logging directory does not exist

今天在裝置上運行相本相關的程式時,出現了

[PLLogging] ***** Error: logging directory does not exist /var/mobile/Library/Logs/CrashReporter/DiagnosticLogs/

的錯誤訊息,雖然看起來似乎是對程式沒有什麼影響,但是同樣的程式已經運行了好幾次了,今天是第一次出現這樣的訊息,而且是在我重新插拔裝置之後發生的,所以還是有點在意。

搜尋了一下,發現似乎是Xcode的Bug,只要將裝置重新啟動,就可以解決囉!大家如果有碰到這個錯誤訊息,就不用太緊張喔~!

相關文章:
  • Stack Overflow:link

2014年10月28日 星期二

[Xcode] 錯誤:"dyld: Symbol not found: _swift_isaMask"

更新了Xcode 6.1之後,突然冒出了"dyld: Symbol not found: _swift_isaMask"的錯誤訊息,導致程式完全無法運行 (它崩潰...我更崩潰啦 ... Orz)

到處檢查、亂調後,並沒有改善,上網查查有沒有其他苦主,終於得到解答啦!趕緊上來記錄分享一下~

救世主文章:https://stackoverflow.com/questions/25957086/running-from-xcode-6-1-linker-errors/26130574#26130574?newreg=e20d94a294ef4a0aad1a00673d5b5ce5

照著第一個解答的快捷鍵,清了一下專案,真的就解決這個問題了!!!(那我之前花的時間是... ...搥地板)

不使用快捷鍵的話,也可以在Product標籤中看到Clean的選項~ (^_^)

2014年8月1日 星期五

[iOS] 調整系統音量(System Volume)_ Swift

因為有網友提出討論,所以就花了點時間研究了一下,順便練習把Objective-C的程式碼改寫成Swift,有需要的人可以參考一下喔!寫法上是有一些差異,但是應該看得懂,也比較好轉換回去Objective-C(因為比較熟咩~),如果是需要Objective-C的版本,可以參考「相關文章」,本篇文章較之前多了監聽實體按鍵的部分。

接下來,只要Copy & Past(喂!沒個正經)就可以獲得一個可互動、調整系統音量的ScrollBar,以及一個當使用者按下機身左側的音量實體按鈕時,會跟著顯示當前音量數值的Label(ScrollBar本身就會跟著動,但是有時候我們會有心想把它隱藏起來XD)

介面安置
不過在這之前,我們需要先在storyboard上安置一個view以及一個label,然後與你的程式碼作連結,如下。
// Storyboard上的一些介面元素連結
@IBOutlet var mpVolumeViewParentView: UIView
@IBOutlet var volumeLabel: UILabel
加入Framework
在專案中加入兩個Framework:MediaPlayer.Framework以及AVFoundation.framework,並且匯入檔案,如下。
import MediaPlayer
import AVFoundation
音樂準備
將要播放的音樂拉進專案中,記得將copy的那個選項勾起來。(在完整程式碼中標示出的紅字,麻煩請改成此音樂的檔名跟檔案格式~)

以上就完成準備動作,接下來就不多說,直接來看完整的程式碼囉!

完整程式碼
import UIKit
import MediaPlayer
import AVFoundation

class ViewController: UIViewController {
    // Storyboard上的一些介面元素連結
    @IBOutlet var mpVolumeViewParentView: UIView
    @IBOutlet var volumeLabel: UILabel

    let volumeView = MPVolumeView()
    var audioPlayer = AVAudioPlayer()
    let musicPlayerController = MPMusicPlayerController()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 把volumeView實體顯示在舞台上設置的UIView(mpVolumeViewParentView)裡面,並且把大小設置成與其相同
        mpVolumeViewParentView.backgroundColor = UIColor.clearColor();
        volumeView.frame = mpVolumeViewParentView.bounds
        mpVolumeViewParentView.addSubview(volumeView)
        
        // 播放音樂
        var music = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("UetoAya", ofType: "mp3"))
        var error:NSError?
        audioPlayer = AVAudioPlayer(contentsOfURL: music, error: &error)
        audioPlayer.prepareToPlay()
        audioPlayer.play()
        
        // 監聽音量實體按鈕
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "getInstanceVolumeButtonNotification:", name:"AVSystemController_SystemVolumeDidChangeNotification", object: nil)
    }
    
    // 接收到音量改變處理
    func getInstanceVolumeButtonNotification(notification: NSNotification) {
        
        var info = notification.userInfo
        var volume = info["AVSystemController_AudioVolumeNotificationParameter"] as NSValue
        println("音量改變:\(volume)")
        
        showCurrentVolumeText(volume)
    }
    
    // 顯示音量文字
    func showCurrentVolumeText(currentVolume:NSValue) {
        
        self.volumeLabel.text = "\(currentVolume)"
    }

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

}

運作起來長這樣


外部參考資料/相關連結:
  • 獲取音量實體按鍵事件的方法:link
  • 監聽案件音量:link
  • NSNotification相關的資料:link
相關文章:
  • [iOS] 調整系統音量(System Volume)_ Objective-C:link