必须有更好的方法来填充UIScrollView
UILabel
和UIImageView
以及其他内容,而无需使用起始位置定义CGRectMake
。堆叠视图有这样的东西吗?我假设不是真的,但是动态添加内容的正确方法是什么。
以下我正在做的事情:
func configureView() {
self.addHeaderImage()
var scrollView: UIScrollView!
var missionStatement: UILabel!
var missionStatementText: UILabel!
var history: UILabel!
var historyText: UILabel!
scrollView = UIScrollView()
scrollView!.frame = CGRectMake(0, headerImage.bounds.height, self.view.bounds.width, self.view.bounds.height)
scrollView!.contentSize = CGSize(width: self.view.bounds.width, height: self.view.bounds.height * 2)
scrollView!.autoresizingMask = UIViewAutoresizing.FlexibleHeight
self.view.addSubview(scrollView)
missionStatement = UILabel()
missionStatement!.frame = CGRectMake(10, 5, headerImage.bounds.width - 20, 30)
missionStatement!.text = "Our Mission Statement"
missionStatement!.font = UIFont(name: "HelveticaNeue-Bold", size: 22.0)
scrollView.addSubview(missionStatement)
missionStatementText = UILabel()
missionStatementText!.frame = CGRectMake(10, missionStatement.frame.height, headerImage.bounds.width - 20, 100)
missionStatementText!.text = "Covenant United Methodist Church, as a companionate community, serves people through the sharing, caring, and reaching out in the name of Jesus Christ."
missionStatementText!.font = UIFont(name: "HelveticaNeue", size: 17.0)
missionStatementText!.numberOfLines = 0
scrollView.addSubview(missionStatementText)
history = UILabel()
// By the time Im done adding things to this 1 view, Its going to be an insane addition problem to find the starting y of the rectangle
history!.frame = CGRectMake(0, missionStatement.frame.height, 0, 0)
}
答案 0 :(得分:1)
您可以为UIView写一个扩展名
this.AMOUNT_RECEIVED = parseFloat(data.AMOUNT_RECEIVED);
并编写func来调整Scroll内容视图的大小,如果视图highet大于滚动视图,则使其可滚动
extension UIView {
func addSubviewAtBottomOfAllSubviews(view:UIView){
var maxY = CGFloat(0)
for subview in self.subviews{
if maxY < subview.frame.maxY{
maxY = subview.frame.maxY
}
}
view.frame.origin.y = maxY
self.addSubview(view)
}
}
使用:
extension UIScrollView{
func resizeContentSize(){
var contentRect = CGRectZero;
for view in self.subviews{
contentRect = CGRectUnion(contentRect, (view ).frame);
}
self.contentSize = contentRect.size;
}
}
答案 1 :(得分:0)
@Daniel Krom答案的改进和扩展:
fileprivate extension UIView {
enum SubviewPosition {
case Top
case Bottom
}
func addSubview(_ newView :UIView, relativeToOtherSubviewsAt position: SubviewPosition) {
newView.frame.origin.y = CGFloat(0)
newView.translatesAutoresizingMaskIntoConstraints = false
if position == .Top {
self.addSubview(newView)
for subview in self.subviews {
subview.frame.origin.y += newView.frame.size.height
}
} else if position == .Bottom {
//finding last subview
var lastSubview = newView
for subview in self.subviews {
if subview.frame.maxY > lastSubview.frame.maxY {
lastSubview = subview
newView.frame.origin.y = subview.frame.maxY
}
}
self.addSubview(newView)
if lastSubview != newView {
newView.topAnchor.constraint(equalTo:lastSubview.bottomAnchor).isActive = true
}
}
}
}
fileprivate extension UIScrollView {
func updateContentSize() {
let contentRect: CGRect = subviews.reduce(into: .zero) { rect, view in
rect = rect.union(view.frame)
}
contentSize = contentRect.size
}
}
用法:
scrollView.addSubview(customView, relativeToOtherSubviewsAt: .Top)
scrollView.addSubview(customView, relativeToOtherSubviewsAt: .Bottom)