我在我的故事板中创建了一个具有任何宽度和任何高度的 UIScrollview,并且我试图将内容大小宽度设置为查看它的设备上框架的宽度。无论我尝试什么,宽度都设置为故事板上显示的宽度 584,即使在 iPhone 6 模拟器上运行它时,它也应该在 300 左右。这导致滚动视图能够滚动水平,这是我不想要的。我确实设法通过添加 UIView 作为子视图来让滚动视图停止水平滚动。但是,我仍然无法弄清楚如何获得正确的宽度尺寸。我不仅试图设置内容大小宽度,而且还试图在滚动视图内设置一个 UILabel,其宽度与 uiscrollview 的内容大小相同,但是获取滚动视图和 UIView 子视图的框架宽度或边界宽度太大。对此的任何帮助将不胜感激。
滚动视图位于自定义表格视图单元格内。这是单元格的类。
import UIKit
class CustomTableViewCell: UITableViewCell, UIScrollViewDelegate {
@IBOutlet weak var winLoseValueLabel: UILabel!
@IBOutlet weak var dateLabel: UILabel!
@IBOutlet weak var newTotalLabel: UILabel!
@IBOutlet weak var locationLabel: UILabel!
@IBOutlet weak var playersScrollView: CustomTableCellScrollView!
@IBOutlet weak var scrollContentView: UIView!
var numLinesInScrollView : CGFloat?
var tblView : UITableView?
var people : String?
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
self.playersScrollView.tblView = tblView
numLinesInScrollView = 0.0
//self.playersScrollView.contentSize.width = self.scrollContentView.frame.width
self.playersScrollView.contentSize.width = self.playersScrollView.frame.size.width
self.playersScrollView.contentSize.height = 100
//self.playersScrollView.contentSize.width = UIScreen.mainScreen().bounds.width - 24
self.playersScrollView.addSubview(self.scrollContentView)
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
func addLabelToScrollView(str : String) {
// Increment the number of lines in the scrollview
if numLinesInScrollView != nil {
numLinesInScrollView!++
}
else{
numLinesInScrollView = 1
}
// Get the bounds of the screen
let screenSize : CGRect = UIScreen.mainScreen().bounds
// Update the height of the scrollview
self.playersScrollView.contentSize.height = 20 * numLinesInScrollView!
// Add a new label to the players scroll view
let w : CGFloat = self.scrollContentView.frame.width - 350
let h : CGFloat = 20
let x : CGFloat = 0
let y : CGFloat = (numLinesInScrollView! - 1) * h
let frame : CGRect = CGRect(x: x, y: y, width: w, height: h)
var person : UILabel = UILabel(frame: frame)
person.font = UIFont(name: "HelveticaNeue-Bold", size: 12.0)
person.textColor = UIColor.blackColor()
person.textAlignment = NSTextAlignment.Center
switch numLinesInScrollView!{
case 1:
person.backgroundColor = UIColor(red: 1.0, green: 0, blue: 0, alpha: 1.0)
case 2:
person.backgroundColor = UIColor(red: 0, green: 1.0, blue: 0, alpha: 1.0)
case 3:
person.backgroundColor = UIColor(red: 0, green: 0, blue: 1.0, alpha: 1.0)
case 4:
person.backgroundColor = UIColor(red: 1.0, green: 0, blue: 1.0, alpha: 1.0)
default:
break
}
person.text? = "Hellow World"
self.scrollContentView.addSubview(person)
}
}