gpt4 book ai didi

ios - 无法找到一种方法以编程方式将我的所有 View 添加到 UIScrollview 中

转载 作者:行者123 更新时间:2023-11-30 12:01:54 25 4
gpt4 key购买 nike

**这是我以编程方式制作的 ui 代码,但我无法使其滚动 **

这是我的代码,我尝试了很多方法以编程方式添加 ScrollView ,但没有成功,请帮忙

 var cityname_EN: String?
var cityname_DE: String?
var country: String?
var text_EN: String?
var text_DE: String?

var scrollView: UIScrollView!
var imageView: UIImageView!

let labelOne: UILabel = {
let label = UILabel()
label.text = "Scroll Top"
label.backgroundColor = .red
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()

let labelTwo: UILabel = {
let label = UILabel()
label.text = "Scroll Bottom"
label.backgroundColor = .green
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()



let allImageView: UIImageView = {
let imageView = UIImageView(image: #imageLiteral(resourceName: "all"))
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.contentMode = .scaleAspectFit
return imageView
}()


let citynameELable:UILabel = {

let citynameELable = UILabel()


citynameELable.textColor = UIColor.darkText
citynameELable.textAlignment = .center
citynameELable.font = UIFont.boldSystemFont(ofSize: 18)
citynameELable.translatesAutoresizingMaskIntoConstraints = false

return citynameELable

}()

let countryLable:UILabel = {

let countryLable = UILabel()


countryLable.textColor = UIColor.darkText
countryLable.textAlignment = .center
countryLable.font = UIFont.boldSystemFont(ofSize: 18)
countryLable.translatesAutoresizingMaskIntoConstraints = false

return countryLable

}()


let textELable: UITextView = {

let textELable = UITextView()
textELable.translatesAutoresizingMaskIntoConstraints = false
textELable.textAlignment = .center
textELable.font = UIFont.boldSystemFont(ofSize: 12)
textELable.isEditable = false
textELable.isScrollEnabled = false
return textELable
}()


override func viewDidLoad() {
super.viewDidLoad()

view.backgroundColor = UIColor.white
setupNavigation()
setupLayout()


}




func setupNavigation(){

navigationItem.title = "Map View"
navigationController?.navigationBar.prefersLargeTitles = true
navigationItem.hidesSearchBarWhenScrolling = false

navigationController?.navigationBar.largeTitleTextAttributes =
[NSAttributedStringKey.foregroundColor: UIColor.gray,
NSAttributedStringKey.font: UIFont(name: "Arial", size: 30) ??
UIFont.systemFont(ofSize: 30)]

}



private func setupLayout()
{

view.addSubview(countryLable)
view.addSubview(citynameELable)
view.addSubview(textELable)

citynameELable.text = cityname_EN
countryLable.text = country

textELable.text = text_EN

let topMapBokContainerView = UIView()
topMapBokContainerView.backgroundColor = UIColor.red

topMapBokContainerView.translatesAutoresizingMaskIntoConstraints = false

let mySegmentedControl = UISegmentedControl (items: ["English","DE"])




mySegmentedControl.translatesAutoresizingMaskIntoConstraints = false

mySegmentedControl.accessibilityActivate()

mySegmentedControl.isEnabledForSegment(at: 0)

mySegmentedControl.selectedSegmentIndex = 0

// Add function to handle Value Changed events
mySegmentedControl.addTarget(self, action: #selector(self.segmentedValueChanged(_:)), for: .valueChanged)




let bottomControlsStackView = UIStackView(arrangedSubviews: [citynameELable,countryLable])
bottomControlsStackView.backgroundColor = UIColor.blue
bottomControlsStackView.translatesAutoresizingMaskIntoConstraints = false
bottomControlsStackView.distribution = .fillEqually





view.addSubview(topMapBokContainerView)




topMapBokContainerView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
topMapBokContainerView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
topMapBokContainerView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
topMapBokContainerView.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.5).isActive = true



topMapBokContainerView.addSubview(allImageView)


allImageView.centerXAnchor.constraint(equalTo: topMapBokContainerView.centerXAnchor).isActive = true
allImageView.centerYAnchor.constraint(equalTo: topMapBokContainerView.centerYAnchor).isActive = true
allImageView.heightAnchor.constraint(equalTo: topMapBokContainerView.heightAnchor, multiplier: 0.5).isActive = true



self.view.addSubview(mySegmentedControl)
NSLayoutConstraint.activate([
mySegmentedControl.topAnchor.constraint(equalTo: topMapBokContainerView.safeAreaLayoutGuide.bottomAnchor, constant: 10 ),

mySegmentedControl.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 10),
mySegmentedControl.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -10),
mySegmentedControl.heightAnchor.constraint(equalToConstant: 30)

])

view.addSubview(bottomControlsStackView)

NSLayoutConstraint.activate([
bottomControlsStackView.topAnchor.constraint(equalTo: mySegmentedControl.safeAreaLayoutGuide.bottomAnchor, constant: 10 ),

bottomControlsStackView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 10),
bottomControlsStackView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -10),
bottomControlsStackView.heightAnchor.constraint(equalToConstant: 50)

])

view.addSubview(textELable)

textELable.topAnchor.constraint(equalTo: bottomControlsStackView.safeAreaLayoutGuide.bottomAnchor).isActive = true
textELable.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 10).isActive = true
textELable.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -10).isActive = true
textELable.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: 0).isActive = true


}

@objc func segmentedValueChanged(_ sender:UISegmentedControl!)
{

if sender.selectedSegmentIndex == 0 {

citynameELable.text = cityname_EN
textELable.text = text_EN
}
else if sender.selectedSegmentIndex == 1 {

citynameELable.text = cityname_DE
textELable.text = text_DE

}


}





override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

}

我创建的所有 View 都尝试添加到 ScrollView ,例如 view.addSubView(MyViews Here)

Here is the screen shot of my app

最佳答案

  1. 为引用 SuperView 的 ScrollView 设置顶部、底部、前导和尾随约束
  2. super View 添加 ScrollView (view.addSubview(scrollView))
  3. 创建内容 View 并设置内容 View 引用 ScrollView 的顶部、底部、前导和尾随约束
  4. 将内容 View 添加到 ScrollView (scrollView.addSubview(contentView))
  5. 设置内容 View 中的元素

关于ios - 无法找到一种方法以编程方式将我的所有 View 添加到 UIScrollview 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47116436/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com