在ScrollView中的UIView,SegmentedController和UIContainerView或类似的东西?
在我的故事板中,我有一个VC包含顶部的UIView,中间的分段控制器和底部的2个ContainerViewControllers,嵌入了新的VC的segue.
在新的VC中我有一个TableView和一个CollectionView(例如,Instagram的个人资料).
我的问题是我需要SegmentedController和UIView使用ContainerView(TableView/CollectionView)滚动,此时只有ContainerView部分滚动并且上面的部分是固定的.
现在我猜我可能需要将所有内容放入UIScrollView中,所以我尝试了并正确放置了所有的成本.但是当它在Swift文件中进行配置时,我现在只是如何设置滚动的高度,但是我需要的高度显然可以变化并且不是固定的高度!
如果有人可以在这里帮助我,这将是非常棒的,或者可能指向我这里已经问过的类似问题?我已经看了,但遗憾的是没找到任何东西!
下面的图片基本上解释了我所追求的内容,如果上面我不是很清楚..
这是一个你可以看到的例子:https://github.com/Jackksun/ScrollIssue
我一直在玩弄动态UICollectionViewCell的和已经注意到,在iOS 8调用cell.contentView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize)上UICollectionViewCell返回不正确的宽度.目前解决此问题的唯一解决方法是显式添加宽度约束以强制单元格的宽度.以下代码用于Cell子类:
func calculatePreferredHeightForFrame(frame: CGRect) -> CGFloat {
var newFrame = frame
self.frame = newFrame
let widthConstraint = NSLayoutConstraint(item: contentView, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: CGRectGetWidth(frame))
contentView.addConstraint(widthConstraint)
self.setNeedsUpdateConstraints()
self.updateConstraintsIfNeeded()
self.setNeedsLayout()
self.layoutIfNeeded()
let desiredHeight: CGFloat = self.contentView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height
newFrame.size.height = CGFloat(ceilf(Float(desiredHeight)))
contentView.removeConstraint(widthConstraint)
return CGRectGetHeight(newFrame)
}
Run Code Online (Sandbox Code Playgroud)
我知道,对于iOS 8和动态UICollectionViewFlowLayout,UICollectionViewCell的contentView以不同的方式处理约束,但是我在这里缺少什么?为确保systemLayoutSizeFittingSize在单元格上使用特定宽度,需要做些什么?
我也遇到过这篇文章(使用自动布局在UICollectionView中指定单元格的一个维度)并且相信这可能实际上使我的问题无效.也许UICollectionViewFlowLayout不是为只有一个动态维度的单元格设计的,但仍然无法解释为什么单元格给出了不寻常的宽度.
我试图让它UICollectionView无限滚动.想法是,当你到达底部的数据阵列时,它重新开始.
我这样做是通过返回一个更大的数字numberOfItemsInSection然后做一个%从数组中获取数据.
这很好,我理解:
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 500
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellIdentifier, forIndexPath: indexPath) as! PhotoCell
let index = indexPath.item % photos.count
let url = photos[index]
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,这是实现此功能的最佳方式吗?我一直在网上无休止地环顾四周,找不到任何关于如何做的其他建议(使用时UICollectionView).
我试图理解为什么Collection View保持中心对齐集合中的最后一个单元格.
我创建了一个简单的基于Flow布局的集合视图.我正在使用Autolayout标志 - 我不确定是否会导致此问题.
每当我从Collection视图中删除一个单元格时 - 前几个似乎工作正常并向左滚动.然而,当我移除倒数第二个时,突然最后一个单元从左对齐变为中心对齐.有人有解释吗?这看起来很奇怪.
如何制作它以使所有单元格向左滚动并保持与左对齐.
编辑:这是视图层次调试:http: //imgur.com/a/NxidO
这是视图行为
我已经制作了一个简单的github来演示它:https://github.com/grantkemp/CollectionViewIssue
这是代码:
var dataforCV = ["short","longer phrase", "Super long Phrase"]
override func viewDidLoad() {
super.viewDidLoad()
demoCollectionView.reloadData()
let layout = UICollectionViewFlowLayout()
// Bug Description - > UICollectionViewFlowLayoutAutomaticSize will center Align the layout if it has only a single cell - but it will left align the content if there is more than one cell.
// I would expect this behaviour to be consistently left …Run Code Online (Sandbox Code Playgroud) 下午好。
我已经在我的 iOS 应用程序的这个“功能”上苦苦挣扎了几个小时,我需要一些帮助。
问题:我应该如何实现,以便当用户输入UITextView尺寸的增加(仅底部边距)并且单元格增加其高度以UITextView同时动态适应?我不知道如何解决它。
研究: 经过一番搜索后,我发现在键入文本时动态更改单元格的高度,并重新加载包含的表格视图以调整大小以及 您可以在选择时对 UITableViewCell 上的高度变化进行动画处理吗? 我还阅读了https://developer.apple.com/documentation/uikit/uicollectionview但我觉得到目前为止我的快速知识不如这些帖子中的预期。
换句话说,我不知道如何为我的 UICollectionView 实现它。
UITextView我的代码现在看起来像这样(我在 EventsCell 中有更多 UI 元素,但将它们剪掉以节省空间,因此它只是我想要更改的底部边距)。我已经将 RootCell 设置delegate为我的UITextView,然后我打算在编辑时获取 textView 高度,以获得我想要的高度,但它不起作用。
细胞类别:
class EventsCell: UICollectionViewCell {
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = UIColor.white
setupViews()
}
let textView: GrowingTextView = { // GrowingTextView is a cocoapod extending UITextView
let rootCellDelegate = RootCell()
let tv = GrowingTextView()
tv.backgroundColor = .clear
tv.allowsEditingTextAttributes = true
tv.isScrollEnabled …Run Code Online (Sandbox Code Playgroud) TL; DR
尝试通过自动布局调整UICollectionViewCells的大小时,即使是一个简单的示例,您也可以轻松获得自动布局警告.
我们应该contentView.translatesAutoResizingMaskToConstraints = false摆脱它们吗?
我正在尝试使用自定义自动布局单元格创建UICollectionView .
在viewDidLoad中:
let layout = UICollectionViewFlowLayout()
layout.estimatedItemSize = CGSize(width: 10, height: 10)
collectionView.collectionViewLayout = layout
Run Code Online (Sandbox Code Playgroud)
我的细胞很基础.它是一个宽度和高度为75的蓝色视图,用于测试目的.通过将视图固定到所有4个边上的超视图,并为其指定高度和宽度来创建约束.
class MyCell: UICollectionViewCell {
override init(frame: CGRect) {
view = UIView()
super.init(frame: frame)
view.backgroundColor = UIColor.blueColor()
contentView.addSubview(view)
installConstraints()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
var view: UIView
func installConstraints() {
view.translatesAutoresizingMaskIntoConstraints = false
var c: NSLayoutConstraint
// pin all edges
c = NSLayoutConstraint(item: contentView, attribute: .Leading, relatedBy: …Run Code Online (Sandbox Code Playgroud) 我正在使用 newUICollectionViewListCell和UIContentConfigurations,它们非常棒,但我不知道如何更改UIKit自动为这些单元格设置的默认单元格高度 44。这是一个示例单元格:
class TextFieldCell: UICollectionViewListCell {
struct ContentConfiguration: UIContentConfiguration, Hashable {
struct Handlers: ContentConfigurationHandlers {
var onTextChanged: ((_ newText: String) -> Void)?
}
var handlers: Handlers?
var text: String?
var placeholder: String?
var keyboardType: UIKeyboardType?
func makeContentView() -> UIView & UIContentView {
ContentView(configuration: self)
}
func updated(for state: UIConfigurationState) -> TextFieldCell.ContentConfiguration {
guard let state = state as? UICellConfigurationState else { return self }
var updatedConfiguration = self
return updatedConfiguration
}
}
class …Run Code Online (Sandbox Code Playgroud) 因为UICollectionViews,有多种细胞类型可能吗?
例如:
media_cell
regular_cell
ad_cell
Run Code Online (Sandbox Code Playgroud)
您是否只需要注册它们,或者您是否必须包含if语句并根据某个变量更改布局以实现此效果.
例如:
if cell.ad == true {
}
Run Code Online (Sandbox Code Playgroud)
原因是,我想要一个略有不同大小的单元格来存在图像.我想重新调整单元格可以工作,但我没有在网上看到任何东西.
有什么建议
我正在使用Autolayout和UICollectionView的AutoSizing单元格.
我可以在单元初始化的代码中指定约束:
func configureCell() {
snp.makeConstraints { (make) in
make.width.equalToSuperview()
}
}
Run Code Online (Sandbox Code Playgroud)
然而,应用程序崩溃,因为单元格尚未添加到collectionView.
问题
在cell生命周期的哪个阶段,可以用cell's width?添加约束?
有没有作出任何默认方式cell的widthequal to the
宽of the的CollectionView without accessing an instance of
UIScreen orUIWindow`?
编辑 问题不重复,因为它不是关于如何使用AutoSizing单元格功能,而是在单元生命周期的哪个阶段应用约束以在使用AutoLayout 时实现所需的结果.
我正在将UICollectionView与AutoSizing单元格一起使用,其外观类似于UITableView。
正如前面的问题中提到的:q1,q2采用“自动调整大小”单元需要preferredLayoutAttributesFitting(_:)在的帮助下实现应用调整大小的方法systemLayoutSizeFitting(_ targetSize:)。
但是,在最近的WWDC会话(高性能自动布局)中,有人提到使用起来systemLayoutSizeFitting(_ targetSize:)很昂贵,因为它会创建一个新的自动布局引擎,然后将其丢弃,要求它解决所有约束,而不缓存先前计算的结果。
在我的应用中,我已经按照答案中提出的方式实现了自动调整大小。但是,在类似表格的列表(许多小高度的行)中,滚动性能非常糟糕。
显然,呼吁systemLayoutSizeFitting(_ targetSize:)在UICollectionViewCell刚刚计算出它的尺寸是昂贵而费力的CPU(因为规模估计和主线程的实际布局走位都)上。
建议同时实现以下结果的方法是什么
UICollectionView Width minus margins?同时具有AutoSizing单元和高滚动性能的推荐策略是什么?
ios ×10
swift ×7
autolayout ×5
autosize ×1
uiscrollview ×1
uitableview ×1
uitextview ×1
uiview ×1
xcode ×1
xcode6 ×1