我想在scrollview中添加三个视图。这些观点必须像Snapchat一样彼此相邻。但是,当我在另一个大小的设备上启动我的应用程序时,视图不适应。有人知道我该如何解决这个问题?
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
let MainEuroView = storyboard.instantiateViewController(withIdentifier: "MainViewController") as! MainViewController
let TicketEuroView = storyboard.instantiateViewController(withIdentifier: "TicketViewController") as! TicketViewController
let ProfilView = storyboard.instantiateViewController(withIdentifier: "ProfilViewController") as! ProfilViewController
var TicketViewFrame : CGRect = TicketView.view.frame
TicketViewFrame.origin.x = 0
TicketView.view.frame.size = CGSize(width: self.view.frame.width, height: self.view.frame.size.height)
TicketView.view.frame = TicketViewFrame
var MainViewFrame : CGRect = MainView.view.frame
MainViewFrame.origin.x = self.view.frame.width
MainView.view.frame = MainViewFrame
var ProfilViewFrame : CGRect = ProfilView.view.frame
ProfilViewFrame.origin.x = 2 * self.view.frame.width
ProfilView.view.frame = ProfilViewFrame
self.addChildViewController(TicketView)
self.main_Scroll_View.addSubview(TicketView.view)
TicketView.didMove(toParentViewController: self)
self.addChildViewController(MainView)
self.main_Scroll_View.addSubview(MainView.view)
MainView.didMove(toParentViewController: self)
self.addChildViewController(ProfilView)
self.main_Scroll_View.addSubview(ProfilView.view)
ProfilView.didMove(toParentViewController: self)
self.main_Scroll_View.contentSize = CGSize(width: self.view.frame.width * 3, height: self.view.frame.size.height)
答案 0 :(得分:0)
快速解决方法是将帧调整移至viewDidLayoutSubviews
方法,您通常在viewDidLoad
期间没有视图帧,而是在故事板中获取视图的帧,例如
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
var TicketViewFrame : CGRect = TicketView.view.frame
TicketViewFrame.origin.x = 0
TicketView.view.frame.size = CGSize(width: self.view.frame.width, height: self.view.frame.size.height)
TicketView.view.frame = TicketViewFrame
var MainViewFrame : CGRect = MainView.view.frame
MainViewFrame.origin.x = self.view.frame.width
MainView.view.frame = MainViewFrame
var ProfilViewFrame : CGRect = ProfilView.view.frame
ProfilViewFrame.origin.x = 2 * self.view.frame.width
ProfilView.view.frame = ProfilViewFrame
}
更好的解决方法是实现自动布局,这将自动为您执行此操作。