2016-01-21 64 views
0

我正在學習自動佈局的過程中,我使用snapkit。我編寫了一些代碼,但工作方式與預期不同。我編寫代碼leftMargin,但它的工作方式好像它是一個正確的Margin。你可以在照片上看到。我的代碼有什麼問題?AutoLayout錯誤地呈現子視圖的位置

enter image description here

我的代碼

let container = View() 
container.backgroundColor=UIColor.greenColor() 

let v1 = View() 
v1.backgroundColor=UIColor.blackColor() 
self.view.addSubview(container); 
     container.addSubview(v1) 

let padding2 : UIEdgeInsets = UIEdgeInsetsMake(20,20,20,20) 


     container.snp_makeConstraints { (make) -> Void in 


      make.top.equalTo(self.view).offset(padding2.top) 

      make.bottom.equalTo(self.view).offset(-padding2.bottom) 

      // make.left.equalTo(self.view).inset(padding2.left) 

      make.left.equalTo(self.view).offset(padding2.left) 

      make.right.equalTo(self.view).offset(-padding2.right) 

      //make.width.equalTo(self.view.bounds.width-90) 

      /* 
      make.top.equalTo(self.view).offset(20) 
      make.left.equalTo(self.view).offset(20) 
      make.bottom.equalTo(self.view).offset(-20) 
      make.right.equalTo(self.view).offset(-20) 
      */ 
     } 


     let padding : UIEdgeInsets = UIEdgeInsetsMake(50, 50, 15, 10) 




     v1.snp_makeConstraints { (make) -> Void in 


     make.topMargin.equalTo(container).offset(padding.top); 
     make.leftMargin.equalTo(container).offset(padding.left); 

      make.width.equalTo(100); 
      make.height.equalTo(100); 
     } 

回答

1

更換

make.topMargin.equalTo(container).offset(padding.top); 
make.leftMargin.equalTo(container).offset(padding.left); 

make.top.equalTo(container).offset(padding.top); 
make.left.equalTo(container).offset(padding.left); 

leftleftMargin?檢查此:https://stackoverflow.com/a/28692783/3331750

+0

耶運作良好。謝謝你。但@justlike snapkit好不好?如果我專注於它,我會遇到問題或好的圖書館? –

+0

@ErhanDemirci snapkit與Masonry(OC)一樣酷炫。 – Justlike

1

我從來沒有使用snapkit,但在常規的自動佈局,你可以通過以下方式實現你想要的。

let container = UIView() 
    container.backgroundColor=UIColor.greenColor() 

    let v1 = UIView() 
    v1.backgroundColor=UIColor.blackColor() 

    self.view.addSubview(container) 
    container.addSubview(v1) 

    // enable the views to be resized by auto layout 
    container.translatesAutoresizingMaskIntoConstraints = false 
    v1.translatesAutoresizingMaskIntoConstraints = false 

    // pin the container to all edges of the main view 
    container.topAnchor.constraintEqualToAnchor(topLayoutGuide.bottomAnchor).active = true 
    container.bottomAnchor.constraintEqualToAnchor(bottomLayoutGuide.topAnchor).active = true 
    container.trailingAnchor.constraintEqualToAnchor(view.trailingAnchor).active = true 
    container.leadingAnchor.constraintEqualToAnchor(view.leadingAnchor).active = true 

    // anchor the v1 subview to the 
    v1.topAnchor.constraintEqualToAnchor(container.topAnchor, constant: 20).active = true 
    v1.leadingAnchor.constraintEqualToAnchor(container.leadingAnchor, constant: 20).active = true 

    // set a fixed height and width 
    v1.heightAnchor.constraintEqualToConstant(100).active = true 
    v1.widthAnchor.constraintEqualToConstant(100).active = true 

和比例高度和寬度與替換的代碼的最後兩行:

​​
+0

我是ios新手。我正在學習自動佈局。有人說蘋果自動佈局不好。你怎麼看 ?好還是不好 ?我很困惑 。 –

+0

Autolayout很酷。 @ErhanDemirci – Justlike

+0

針對iOS 9及以上版本的@ErhanDemirci使AutoLayout更易於理解和實現,尤其是在編程時。 Btw這些[blogposts](http://sketchytech.blogspot.co.uk/search/label/Fear%20and%20Loathing%20in%20Auto%20Layout)的主題可能會有所幫助。我試圖瞭解它是如何工作的,我寫了這些。 – sketchyTech