【发布时间】:2018-08-25 08:56:11
【问题描述】:
我正在尝试制作一个具有滚动视图和文本视图的视图控制器。我希望仅当文本视图的文本内容很多时才启用滚动。因为如果我可以滚动我的视图控制器看起来很丑,即使文本视图中的文本内容与下图不太相似
我们可以看到视图控制器的底部有一个很大的差距。这是我用于此 VC 的代码
import UIKit
class NotificationDetailVC: UIViewController {
@IBOutlet weak var notificationTitleLabel: UILabel!
@IBOutlet weak var notificationImage: UIImageView!
@IBOutlet weak var notificationDateLabel: UILabel!
@IBOutlet weak var notificationContentTextView: UITextView!
var dataNotification : [String:Any]?
override func viewDidLoad() {
super.viewDidLoad()
updateUI()
updateReadStatusInUserDefault()
}
@IBAction func imageDidTapped(_ sender: Any) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let popup = storyboard.instantiateViewController(withIdentifier: "imageDisplay") as! ReusableImageDisplayVC
popup.photo = notificationImage.image
self.present(popup, animated: true, completion: nil)
}
func updateUI() {
guard let dataNotification = dataNotification else {return}
notificationTitleLabel.text = dataNotification["Judul"] as? String
notificationContentTextView.text = dataNotification["Keterangan"] as? String
guard let notifDate = dataNotification["TglNotif"] as? String else {return}
notificationDateLabel.text = DateTimeService.changeFormat(of: notifDate, toFormat: "d MMM YYY")
let imagePath = dataNotification["Photo"] as? String
guard let encodedImagePath = imagePath?.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed) else {return}
if imagePath != nil {
if imagePath!.isEmpty {
notificationImage.removeFromSuperview()
} else {
notificationImage.sd_setImage(with: URL(string: encodedImagePath), placeholderImage: UIImage(named: "3-2 Img"), options: [.continueInBackground, .progressiveDownload])
}
} else {
notificationImage.removeFromSuperview()
}
}
func updateReadStatusInUserDefault() {
guard let dataNotification = dataNotification,
let notifID = dataNotification["notif_id"] as? Int else {return}
guard var readNotificationStatusUserDefault = UserDefaults.standard.object(forKey: "readNotification") as? [String:String] else {return}
readNotificationStatusUserDefault["\(notifID)"] = "1"
UserDefaults.standard.set(readNotificationStatusUserDefault, forKey: "readNotification")
UserDefaults.standard.synchronize()
}
}
【问题讨论】:
-
您应该根据您的 textview 文本更改滚动视图内容的大小
标签: ios swift uiscrollview uitextview