youngsoft / TangramKit

TangramKit is a powerful iOS UI framework implemented by Swift. It integrates the functions with Android layout,iOS AutoLayout,SizeClass, HTML CSS float and flexbox and bootstrap. So you can use LinearLayout,RelativeLayout,FrameLayout,TableLayout,FlowLayout,FloatLayout,LayoutSizeClass to build your App 自动布局 UIView UITableView UICollectionView
MIT License
1.21k stars 175 forks source link

在UIScrollView中使用TGLinearLayout进行布局,当布局中使用到从xib加载出来的view时,view的高度异常。 #16

Closed ShayneYeorg closed 5 years ago

ShayneYeorg commented 7 years ago

当使用如下代码进行布局后,linearLayout的高度为74(这是正确的),assignView的高度却变成了138(正确应该是64)。大致代码如下:


        let scrollView = UIScrollView()
        scrollView.frame = CGRect(x: 0, y: 64, width: 300, height: 300)
        scrollView.backgroundColor = UIColor.lightGray
        scrollView.showsVerticalScrollIndicator = false
        slef.view.addSubview(scrollView)

        let linearLayout = TGLinearLayout(.vert)
        linearLayout.tg_size(width: .fill, height: .wrap)

        // 从xib里加载出来的view
        let assignView = Bundle.main.loadNibNamed("AssignView", owner: nil, options: nil)!.last as! AssignView
        assignView.tg_size(width: .fill, height: 64)
        assignView.tg_bottom.equal(10)
        linearLayout.addSubview(assignView)

        scrollView.addSubview(linearLayout) 
youngsoft commented 6 years ago

原因是这样的,因为AssignView是从XIB上构建的,而默认的XIB的autoresizingMask的设置是:~UIViewAutoresizingNone 导致当子视图被添加到布局时候后,如果布局视图的尺寸改变的话就会对子视图的尺寸和位置进行重新布局导致的。解决的方法是当将XIB上构建的视图添加到布局视图时将autoresizingMask的值设置为UIViewAutoresizingNone:

let assignView = Bundle.main.loadNibNamed("AssignView", owner: nil, options: nil)!.last as! AssignView
 assignView.autoresizingMask = .none
 assignView.tg_size(width: .fill, height: 64)