【问题标题】:How to expand UICollectionView frame size dynamically in swift如何在swift中动态扩展UICollectionView帧大小
【发布时间】:2015-04-21 11:29:58
【问题描述】:

使用故事板,我添加了 Scrollview -> View -> 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
    }

第二次尝试

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.

请指导我。

【问题讨论】:

  • 你是从哪里学会这样命名变量的?
  • 我已编辑。如果您知道,请指导我。
  • 请考虑一个教程,因为我不确定您从哪里获得知识,因为集合视图是滚动视图,因此您无需将其嵌入到 raywenderlich.com/78550/…
  • 你在使用自动布局吗
  • 我正在使用约束。所以我使用自动布局。

标签: swift ios8 uicollectionview


【解决方案1】:

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

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

【讨论】:

  • 那么如何解决我的问题呢?我正在使用约束,所以我不能禁用自动布局。如何展开这个集合视图?
  • @McDonal_11 在您添加约束的每个视图上,您​​应该调用setTranslatesAutoResizingMaskIntoConstraints(false)here 是有关您为什么需要这样做的更多信息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-12-22
  • 2020-07-18
  • 2019-09-04
  • 1970-01-01
  • 2014-07-08
  • 2011-08-28
  • 1970-01-01
相关资源
最近更新 更多