如何在swift中动态扩展UICollectionView框架大小

时间:2015-04-21 11:29:59

标签: swift ios8 uicollectionview

使用storyboard,我添加了Scrollview - >查看 - >的CollectionView。

my_collxn_view框架尺寸为(5,200,310,368), my_view框架尺寸为(0,0,320,568), my_scroll_view框架尺寸为(0,0,320,568)

我有10个细胞。所以内容大小太大。我不知道如何通过编码扩展UICollectionView帧大小。请指导我。 我尝试过一些东西。我的编码如下。

首次尝试

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
    {
        my_collxn_view.frame.size.height = my_collxn_view.contentSize.height //635.0
        my_view.frame.size.height = 200 + my_collxn_view.frame.size.height //835.0

        my_scroll_view.contentSize = CGSizeMake(320, my_view.frame.size.height) //Scrolling
        my_collxn_view.frame = CGRectMake(5, 200, 310, my_collxn_view.frame.size.height) //Not Expanding
        return CGSizeMake(150, 205) //CELL Size
    }

Second Attepmt

override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()


        my_collxn_view.frame.size.height = my_collxn_view.contentSize.height //635.0
        my_view.frame.size.height = 200 + my_collxn_view.frame.size.height  //835.0

        my_collxn_view.frame = CGRectMake(5, 200, 320, my_collxn_view.frame.size.height) //Not Expanding
        my_view.frame = CGRectMake(0, 0, 320, my_view.frame.size.height)  //Not Expanding 
        my_scroll_view.contentSize = CGSizeMake(320, my_view.frame.size.height) //Scrolling
    }

更新

my_view.setTranslatesAutoresizingMaskIntoConstraints(true)
my_collxn_view.setTranslatesAutoresizingMaskIntoConstraints(true)

我使用了以上两行。收到确切的输出。但是,调试区域中显示了一些警告。

警告

2015-04-21 18:43:52.974 E Commerce[9475:1673654] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSIBPrototypingLayoutConstraint:0x7fcf6958a010 'IB auto generated at build time for view with fixed frame' V:|-(200)-[UICollectionView:0x7fcf6a84b000]   (Names: '|':UIView:0x7fcf6957abe0 )>",
    "<NSIBPrototypingLayoutConstraint:0x7fcf6958a0b0 'IB auto generated at build time for view with fixed frame' V:[UICollectionView:0x7fcf6a84b000(368)]>",
    "<NSAutoresizingMaskLayoutConstraint:0x7fcf696745c0 h=--& v=--& UICollectionView:0x7fcf6a84b000.midY == + 517.5>"
)

Will attempt to recover by breaking constraint 
<NSIBPrototypingLayoutConstraint:0x7fcf6958a010 'IB auto generated at build time for view with fixed frame' V:|-(200)-[UICollectionView:0x7fcf6a84b000]   (Names: '|':UIView:0x7fcf6957abe0 )>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2015-04-21 18:43:52.978 Test_work[9475:1673654] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSIBPrototypingLayoutConstraint:0x7fcf6958a0b0 'IB auto generated at build time for view with fixed frame' V:[UICollectionView:0x7fcf6a84b000(368)]>",
    "<NSAutoresizingMaskLayoutConstraint:0x7fcf69674630 h=--& v=--& V:[UICollectionView:0x7fcf6a84b000(635)]>"
)

Will attempt to recover by breaking constraint 
<NSIBPrototypingLayoutConstraint:0x7fcf6958a0b0 'IB auto generated at build time for view with fixed frame' V:[UICollectionView:0x7fcf6a84b000(368)]>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2015-04-21 18:43:52.980 Test_work[9475:1673654] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSIBPrototypingLayoutConstraint:0x7fcf6958a6c0 'IB auto generated at build time for view with fixed frame' V:|-(0)-[UIView:0x7fcf6957abe0]   (Names: '|':UIScrollView:0x7fcf6957a8b0 )>",
    "<NSIBPrototypingLayoutConstraint:0x7fcf6958a760 'IB auto generated at build time for view with fixed frame' V:[UIView:0x7fcf6957abe0(568)]>",
    "<NSAutoresizingMaskLayoutConstraint:0x7fcf69674910 h=--& v=--& UIView:0x7fcf6957abe0.midY == + 442.5>"
)

Will attempt to recover by breaking constraint 
<NSIBPrototypingLayoutConstraint:0x7fcf6958a6c0 'IB auto generated at build time for view with fixed frame' V:|-(0)-[UIView:0x7fcf6957abe0]   (Names: '|':UIScrollView:0x7fcf6957a8b0 )>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2015-04-21 18:43:52.990 Test_work[9475:1673654] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSIBPrototypingLayoutConstraint:0x7fcf6958a760 'IB auto generated at build time for view with fixed frame' V:[UIView:0x7fcf6957abe0(568)]>",
    "<NSAutoresizingMaskLayoutConstraint:0x7fcf69674980 h=--& v=--& V:[UIView:0x7fcf6957abe0(885)]>"
)

Will attempt to recover by breaking constraint 
<NSIBPrototypingLayoutConstraint:0x7fcf6958a760 'IB auto generated at build time for view with fixed frame' V:[UIView:0x7fcf6957abe0(568)]>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

请指导我。

1 个答案:

答案 0 :(得分:0)

Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints

您需要在每个视图上setTranslatesAutoresizingMaskIntoConstraints(false)手动设置一个或多个NSLayoutConstraint