无法在Swift中的自定义UIView上布置子视图 [英] Not able to lay out Subviews on a custom UIView in Swift
问题描述
因此,我创建了这个自定义容器视图,并使用自动布局约束对其进行了布局.
So I have created this custom container view which I am laying out using autolayout constraint.
func configureSegmentContainerView() {
segmentContainerView.topAnchor.constraint(equalTo: view.topAnchor, constant: 40).isActive = true
segmentContainerView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 8).isActive = true
segmentContainerView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -8).isActive = true
segmentContainerView.heightAnchor.constraint(equalToConstant: 3).isActive = true
}
在视图控制器中,viewDidLoad()
是这样的:
In the view controller, the viewDidLoad()
is this:
setupDataSource()
segmentContainerView = ATCStorySegmentsView()
view.addSubview(segmentContainerView)
configureSegmentContainerView()
segmentContainerView.translatesAutoresizingMaskIntoConstraints = false
segmentContainerView.numberOfSegments = friendStory.count
现在,一旦设置了数据源并且我有了friendStory
计数,就将其分配给segmentContainerView.numberofSegments
Now once the data source is setup and I have the friendStory
count, I am assigning it to segmentContainerView.numberofSegments
在segmentContainerview
类中,这是正在发生的事情:
In segmentContainerview
class this is what is happening:
var numberOfSegments: Int? {
didSet {
addSegments()
}
}
在addSegments()
中,我根据n umberOfSegments
添加UIViews,这是该代码:
In addSegments()
, I am adding UIViews depending upon the numberOfSegments
this is the code for that:
private func addSegments() {
guard let numberOfSegments = numberOfSegments else { return }
layoutIfNeeded()
setNeedsLayout()
for i in 0..<numberOfSegments {
let segment = Segment()
addSubview(segment.bottomSegment)
addSubview(segment.topSegment)
configureSegmentFrame(index: i, segmentView: segment)
segmentsArray.append(segment)
}
}
private func configureSegmentFrame(index: Int, segmentView: Segment) {
guard let numberOfSegments = numberOfSegments else { return }
let widthOfSegment : CGFloat = (self.frame.width - (padding * CGFloat(numberOfSegments - 1))) / CGFloat(numberOfSegments)
let i = CGFloat(index)
let segmentFrame = CGRect(x: i * (widthOfSegment + padding), y: 0, width: widthOfSegment, height: self.frame.height)
segmentView.bottomSegment.frame = segmentFrame
segmentView.topSegment.frame = segmentFrame
segmentView.topSegment.frame.size.width = 0
}
**问题和问题:**我没有得到4个UIView,而是得到了3个,但是第三个没有正确放置,并且移出了父容器.我怎样才能使这些uiviews正确对齐.我猜想setNeedsLayout()
和layoutIfNeeded()
需要被调用存在一些问题.请帮忙.
**Question and Issue: ** Instead of getting 4 UIViews, I am getting 3, but the third one is not correctly placed and is going outside the parent container. How can I get these uiviews aligned correctly. I am guessing there is some issue with where setNeedsLayout()
and layoutIfNeeded()
needs to be called. Please help.
Segment
是具有两个属性的结构-bottomSegment和topSegment.两者都是UIView
Segment
is a struct with two properties - bottomSegment and topSegment. Both being UIView
您可以看到只有三个UIView段出现.我需要其中的4个(numberOfSegments
= 4).我也给right and leftAnchor
的父容器常数8和-8.因此所有4个细分都需要放置在此视图中.如您在上图中所看到的,最后一段在父容器之外.
You can see how just three UIView segments appear. I needs to 4 (numberOfSegments
= 4) of these. Also I am giving the parent container constant of 8 and -8 for right and leftAnchor
. so all 4 segments need to be placed within this view. As you can see in the picture above the last segment is going outside of the parent container.
推荐答案
问题是
let widthOfSegment : CGFloat = (self.frame.width ...
被称为太早了.在具有其实际框架之前,您不能执行任何依赖于self
具有其框架的操作.
is being called too soon. You cannot do anything in that depends upon self
having its frame until after it has its real frame.
对于UIView,该时刻是在第一次调用layoutSubviews
之后.
For a UIView, that moment is after layoutSubviews
has been called for the first time.
这篇关于无法在Swift中的自定义UIView上布置子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!