2017-08-07 51 views
2

因此,我已經使用代碼完全編寫了我的應用程序的整個設置vc(沒有故事板),我想知道如何創建您創建不等式約束的約束?這意味着,如果應用程序運行在iPhone 7上,那麼約束條件會與我編碼的一樣,但是當您在iPhone上運行應用程序時,一切都會彼此重疊,而不是正確的比例。我只知道如何在故事板中使用不等式來做到這一點,但是如何在代碼中實現?這是我第一次不使用故事板,所以我有點新。不等式約束以編程方式快速

預先感謝您!

的制約因素是什麼的一些代碼的樣子:

func setupProfileImageView() { 
    //need x, y, width, height constraints 
    profileImageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true 
    profileImageView.topAnchor.constraint(equalTo: view.topAnchor, constant: 120).isActive = true 
    profileImageView.widthAnchor.constraint(equalToConstant: 160).isActive = true 
    profileImageView.heightAnchor.constraint(equalToConstant: 160).isActive = true 

} 
+0

*會按照我編碼爲* ...好吧,如果您實際給了我們一些**代碼**,那將會很好。 – dfd

+0

我很抱歉,我現在編輯它 – Jaqueline

+0

謝謝。我不理解你的意思。你已經定義了(像我這樣的代碼)就夠了。頂部,高度,最後是水平 - 中心X)。什麼是問題? – dfd

回答

0

只需創建兩個約束 - 一個用於最小值(greaterThanOrEqualTo)和一個用於最大值(lessThanOrEqualTo)。

myButton.widthAnchor.constraint(greaterThanOrEqualToConstant: 60).isActive = true 
myButton.widthAnchor.constraint(lessThanOrEqualToConstant: 120).isActive = true 

有一些技巧,如果你需要設置優先級做的,但它不會出現,你需要。如果你已經將置於一切正確(X和Y)這應該做到這一點。

+0

謝謝,這正是我正在尋找的!我試圖找到這個,但我找不到它被稱爲。 – Jaqueline

0

你最簡單的選擇將是留在你的方法,並應用基於算法的常量。例如:

func setupProfileImageView() { 
    //need x, y, width, height constraints 

    var idealTop : CGFloat = view.frame.height * (some value) //so that it equals a value that changes with each screen size in a way that you feel ok with 

    idealTop = idealTop < 60 ? 60 : idealTop //makes sure that the constraint doesnt fall bellow 60.. 

    profileImageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true 
    profileImageView.topAnchor.constraint(equalTo: view.topAnchor, constant: idealTop).isActive = true 
    profileImageView.widthAnchor.constraint(equalToConstant: 160).isActive = true 
    profileImageView.heightAnchor.constraint(equalToConstant: 160).isActive = true 

} 

其次,我會建議爲意味着元素被垂直對齊,並在視圖層次結構的同一水平線上,通過類似的方式疏遠他們任何佈局,並通過確保每個元素的topAnchor指的是它上面的元素bottomAnchor。一個小技巧,你可以做的,就是你的profileImage的topAnchor的值存儲到一個變量,這樣就可以訪問它的價值的其他元素,就像這樣:

var profileImageTopAnchor : NSLayoutConstraint? 

func setupProfileImageView() { 
    //need x, y, width, height constraints 

    var idealTop : CGFloat = view.frame.height * (some value) //so that it equals a value that changes with each screen size in a way that you feel ok with 

    idealTop = idealTop < 60 ? 60 : idealTop //makes sure that the constraint doesnt fall bellow 60.. 

    profileImageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true 
    profileImageTopAnchor = profileImageView.topAnchor.constraint(equalTo: view.topAnchor, constant: idealTop) 
    profileImageTopAnchor.isActive = true //otherwise it wont get activated 
    profileImageView.widthAnchor.constraint(equalToConstant: 160).isActive = true 
    profileImageView.heightAnchor.constraint(equalToConstant: 160).isActive = true 

} 
func setupProfileImageView() { 

    let topAnchor = profileImageTopAnchor != nil ? profileImageTopAnchor!.constant : (/*Some arbitrary value here .. or repeat as above with a fraction value of the view.frame*/) 

    let topAnchorValue : CGFloat = topAnchor * (some value) //this way, the spacing will change based on the spacing you declare on top of your image.. 

    //And say that you have three other elements stacked vertically.. 

    elementA.constraint(equalTo: profileImageView.topAnchor, constant: topAnchorValue).isActive = true 
    elementB.constraint(equalTo: elementA.topAnchor, constant: topAnchorValue).isActive = true 
    elementC.constraint(equalTo: elementB.topAnchor, constant: topAnchorValue).isActive = true 
} 

你可以去進一步進入檢測實際屏幕大小,並將其與列表相關聯,以查看使用哪個手機型號..然後應用特定約束......但隨着此句越來越長,上述方法更直接的原因也越來越多!

+0

非常感謝你這麼詳細的回答!我有點困惑,什麼是「元素a」和其他元素?他們喜歡其他觀點,也許可能是一個標籤?再次感謝!當我在我的電腦上時,我會試試這個,然後回覆你。 – Jaqueline

+0

所以我嘗試了你做的,但它似乎並沒有工作。然而,因爲所有東西都連接到它之前的視圖的底部約束,所以當您更改profileImageView的常量時,所有事情都會完全改變。所以我在想的是有一個函數來確定一個人正在使用的iPhone,然後有一個if語句來改變常量idealTop。但是我不確定語法的樣子是什麼? – Jaqueline

+0

是啊元素a/b/c是不同的意見,UILabel,UIButton等等等等哪些部分似乎不工作? – murphguy