**这是我以编程方式创建的代码,但我无法使其滚动**
这是我的代码我已经尝试了很多方法以编程方式添加滚动视图但是没有用到plz帮助
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.addSubView(MyViews Here)
答案 0 :(得分:0)
答案 1 :(得分:0)
根据您的问题,您的观点层次结构应为:
查看 - > scrollView - > allYourViews 强>
因此,您可以将scrollView框架设置为与视图相同,然后:
view.addSubView(scrollView)
scrollView.addSubView(yourViews Here)