我有一个像这样创建的静态tableView。...
override func viewDidLoad() {
super.viewDidLoad()
usernameLabel.text = "Username"
usernameDisplay.text = "placeholder"
usernameDisplay.textColor = self.view.tintColor
usernameLabel.frame = CGRect(x: self.view.bounds.minX, y: self.usernameCell.bounds.minY, width: self.view.frame.width, height: self.usernameCell.bounds.height)
usernameDisplay.frame = CGRect(x: self.usernameCell.bounds.minX, y: self.usernameCell.bounds.minY, width: self.view.frame.width, height: self.usernameCell.bounds.height)
usernameDisplay.textAlignment = .right
usernameCell.addSubview(usernameLabel)
usernameCell.addSubview(usernameDisplay)
membershipTierLabel.text = "Membership Status"
membershipTierDisplay.text = "Free (0-1GB)"
membershipTierDisplay.textColor = self.view.tintColor
membershipTierLabel.frame = CGRect(x: self.view.bounds.minX, y: self.membershipTierCell.bounds.minY, width: self.view.frame.width, height: self.membershipTierCell.bounds.height)
membershipTierDisplay.frame = CGRect(x: self.view.bounds.minX, y: self.membershipTierCell.bounds.minY, width: self.view.frame.width, height: self.membershipTierCell.bounds.height)
membershipTierDisplay.textAlignment = .right
membershipTierCell.addSubview(membershipTierLabel)
membershipTierCell.addSubview(membershipTierDisplay)
dataUsedLabel.text = "Data Used"
dataUsedDisplay.text = String(UserDefaults.standard.integer(forKey: "total_storage"))
dataUsedDisplay.textColor = self.view.tintColor
dataUsedLabel.frame = CGRect(x: self.view.bounds.minX, y: self.dataUsedCell.bounds.minY, width: self.view.frame.width, height: self.dataUsedCell.bounds.height)
dataUsedDisplay.frame = CGRect(x: self.view.bounds.minX, y: self.membershipTierCell.bounds.minY, width: self.view.frame.width, height: self.membershipTierCell.bounds.height)
dataUsedDisplay.textAlignment = .right
dataUsedCell.addSubview(dataUsedLabel)
dataUsedCell.addSubview(dataUsedDisplay)
tipsText.text = "You can change the color of a progression tag by long-pressing it on the Progressions homepage"
tipsText.frame = CGRect(x: tipsCell.bounds.minX,y: self.tipsCell.bounds.minY,width: tipsCell.bounds.width,height:self.tipsCell.bounds.height)
//tipsText.textColor = UIColor.white
//tipsText.textAlignment = .center
tipsText.backgroundColor = self.view.tintColor
tipsText.font = tipsText.font?.withSize(15)
tipsText.autoresizingMask = [.flexibleWidth, .flexibleHeight]
// tipsText.centerYAnchor.constraint(equalTo: tipsCell.centerYAnchor, constant:0).isActive = true
// tipsText.centerXAnchor.constraint(equalTo: tipsCell.centerXAnchor, constant:0).isActive = true
//tipsText.textContainerInset = UIEdgeInsets(top: 10, left: 5, bottom: 10, right: 5)
tipsCell.backgroundColor = self.view.tintColor
tipsCell.addSubview(tipsText)
tipsText.center = CGPoint(x: tipsCell.bounds.midX, y: tipsCell.bounds.midY)
feedbackText.text = "Feedback is loved! Please email with any issues, comments, ideas, or concerns"
feedbackText.frame = CGRect(x: self.feedbackCell.bounds.minX,y: self.feedbackCell.bounds.minY,width: self.feedbackCell.bounds.width,height:self.feedbackCell.bounds.height)
feedbackText.font = feedbackText.font?.withSize(15)
feedbackCell.addSubview(feedbackText)
//let bytes = UserDefaults.standard.integer(forKey: "total_storage")
//storageAmount.text = format(bytes:Double(bytes))
// Do any additional setup after loading the view.
//myTableView.register(UITableViewCell.self, forCellReuseIdentifier: "MyCell")
myTableView.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height)
myTableView.dataSource = self
myTableView.delegate = self
self.view.addSubview(myTableView)
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.section != 0 {
return 100
} else {
return UITableViewAutomaticDimension
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch(section) {
case 0: return 3 // section 0 has 2 rows
case 1: return 1
case 2: return 1// section 1 has 1 row
default: fatalError("Unknown number of sections")
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch(indexPath.section) {
case 0:
switch(indexPath.row){
case 0:
print("username")
return self.usernameCell
case 1:
return self.membershipTierCell
case 2:
return self.dataUsedCell
default: fatalError("Unknown row")
}
case 1:
switch(indexPath.row){
case 0:
return self.tipsCell
default: fatalError("Unknown row")
}
case 2:
print("feedback")
switch(indexPath.row){
case 0:
print("feedback")
return self.feedbackCell
default: fatalError("Unknown row")
}
default: fatalError("Unknown section")
}
}
}
如您所见,我正在尝试使用tipsText.center = CGPoint(x: tipsCell.bounds.midX, y: tipsCell.bounds.midY)
如您所见,由于tipsText仍在tipsCell的顶部,因此无法正常工作。如何在我想要的位置居中放置它?
答案 0 :(得分:1)
我认为您可以通过两种方式实现自己的愿望。
一种方法是将IB中的tipCell和feedBackCell的高度设置为100。
另一种方法是添加以下代码:
tipsText.textAlignment = .center
tipsText.numberOfLines = 0
tipsText.autoresizingMask = [.flexibleWidth, .flexibleHeight]
feedbackText.textAlignment = .center
feedbackText.numberOfLines = 0
feedbackText.autoresizingMask = [.flexibleWidth, .flexibleHeight]
希望这会有所帮助。
如果是TextView,您可能会获得如下估计的帧数:
tipsText.textContainer.maximumNumberOfLines = 0
tipsText.textContainer.heightTracksTextView = true
tipsText.attributedText = NSAttributedString.init(string: "You can change the color of a progression tag by long-pressing it on the Progressions", attributes: [NSAttributedString.Key.font : UIFont.systemFont(ofSize: 15) ])
tipsText.textAlignment = .center
let size = tipsText.textContainer.size
tipsText.frame = CGRect(x: tipsCell.bounds.minX,y: self.tipsCell.bounds.minY,width: tipsCell.bounds.width, height :size.height )
tipsText.backgroundColor = self.view.tintColor
tipsText.autoresizingMask = [.flexibleWidth, .flexibleHeight]
tipsCell.backgroundColor = self.view.tintColor
tipsCell.addSubview(tipsText)
tipsText.center = CGPoint(x: tipsCell.bounds.midX, y: tipsCell.bounds.midY)
feedbackText.textContainer.maximumNumberOfLines = 0
feedbackText.textContainer.heightTracksTextView = true
feedbackText.attributedText = NSAttributedString.init(string: "Feedback is loved! Please email with any issues, comments, ideas, or concerns", attributes: [NSAttributedString.Key.font : UIFont.systemFont(ofSize: 15) ])
feedbackText.textAlignment = .justified
let feedbackSize = feedbackText.textContainer.size
feedbackText.frame = CGRect(x: feedbackCell.bounds.minX,y: self.feedbackCell.bounds.minY,width: feedbackCell.bounds.width, height :feedbackSize.height )
feedbackText.backgroundColor = self.view.tintColor
feedbackText.autoresizingMask = [.flexibleWidth, .flexibleHeight]
feedbackCell.backgroundColor = self.view.tintColor
feedbackCell.addSubview(feedbackText)
feedbackText.center = CGPoint(x: feedbackCell.bounds.midX, y: feedbackCell.bounds.midY)
答案 1 :(得分:0)
我假设您知道如何使用自定义视图类。 有一个UITextView的自定义类;
class VerticallyCenteredTextView: UITextView {
override var contentSize: CGSize {
didSet {
var topCorrection = (bounds.size.height - contentSize.height * zoomScale) / 2.0
topCorrection = max(0, topCorrection)
contentInset = UIEdgeInsets(top: topCorrection, left: 0, bottom: 0, right: 0)
}
}
}