我无法将scrollView的宽度设置为屏幕宽度。有人可以指导我出错的地方吗?
以下是我启用滚动视图的方法
// SCROLL VIEW
var scrollView = UIScrollView()
var contentView = UIView()
override func viewDidLoad() {
super.viewDidLoad()
screenHeight = screenSize.height
screenWidth = screenSize.width
// SCROLL VIEW
scrollView = UIScrollView(frame: view.bounds)
// Here I am adding all my labels and textview fields to content view
contentView.addSubview(problemDescriptionStackView)
....
// Adding content view to the scrollView
scrollView.addSubview(contentView)
// Pinning the contentView to the scroll view
pinView(contentView, to: scrollView)
self.view.addSubview(scrollView)
// Here I am Setting the constraints for all the items in the contentView
....
}
//以下函数用于将一个视图固定到另一个视图
public func pinView(_ view: UIView, to scrollView: UIScrollView) {
view.translatesAutoresizingMaskIntoConstraints = false
view.pin(to: scrollView)
}
public func pin(to view: UIView) {
NSLayoutConstraint.activate([
leadingAnchor.constraint(equalTo: view.leadingAnchor),
trailingAnchor.constraint(equalTo: view.trailingAnchor),
topAnchor.constraint(equalTo: view.topAnchor),
bottomAnchor.constraint(equalTo: view.bottomAnchor)
])
}
答案 0 :(得分:1)
使用以下方法相应地获取iPhone的精确尺寸。 希望用这种方法,你的问题解决了。如果您有疑问请在这里发表评论。
//MARK: - Main Screen Width
func getScreenWidth() -> CGFloat{
return UIScreen.main.bounds.size.width
}
//MARK: - Main Screen Height
func getScreenHeight() -> CGFloat{
return UIScreen.main.bounds.height
}
答案 1 :(得分:0)
viewDidLoad
从storyboard
或nib
加载您的ui。大小取决于您在Interface Builder
中选择的内容。
您可以在viewWillAppear
中初始化ui并添加变量isInitialized
。
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if !isInitialized {
isInitialized = true
// do your stuff
}
}
答案 2 :(得分:0)
Scroll.contentSize = CGSize(width:self.view.frame.size.width,height:self.view.frame.size.height)
答案 3 :(得分:0)
将一个子视图水平居中(使用自动布局约束)到界面构建器中的滚动视图或通过编程方式
答案 4 :(得分:0)
好的,有几种方法可以解决这个问题:
1.-在故事板中转到您的视图,您将在滚动视图中按住左键并在主视图上将其释放,然后您将点击相等的宽度
2.-在视图中确实加载了
self.scrollViewName.frame.size.width = self.view.frame.width
答案 5 :(得分:0)
你可以在scrollView上设置contentInset,它会调整scrollView中滚动区域的内容大小,这里有一个简单的例子
class PhotoViewController: UIViewController, CLLocationManagerDelegate, UIPickerViewDataSource, UIPickerViewDelegate {
override var prefersStatusBarHidden: Bool {
return true
}
private var backgroundImage: UIImage
private var backgroundImageView: UIImageView?
private var picker: UIPickerView
var list: [String]
var locationPicked: String
var locationArray: [Any]
var partyIndex: Int = Int()
init(image: UIImage) {
self.backgroundImage = image
self.picker = UIPickerView()
self.list = ["Loading..."]
self.locationPicked = list[0]
self.locationArray = []
super.init(nibName: nil, bundle: nil)
print(image)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
/************************************/
override func viewDidLoad() {
super.viewDidLoad()
if Reachability.isConnectedToNetwork() == true{
mapApi()
}
let backgroundImageView = UIImageView(frame: view.frame)
backgroundImageView.image = backgroundImage
backgroundImageView.isUserInteractionEnabled = true
self.backgroundImageView = backgroundImageView
let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(panGestureRecognizerAction(_:)))
backgroundImageView.addGestureRecognizer(panGestureRecognizer)
let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(longTap(sender:)))
backgroundImageView.addGestureRecognizer(longGesture)
view.addSubview(backgroundImageView)
view.backgroundColor = greenColor
picker.dataSource = self
picker.delegate = self
picker.frame = CGRect(x: ((self.view.frame.width)/2)-84, y: (self.view.frame.height)-70, width: 160, height: 40)
picker.clipsToBounds = true
picker.layer.cornerRadius = picker.bounds.size.width/20
picker.layer.borderColor = redColor.cgColor
picker.layer.borderWidth = 0
picker.backgroundColor = UIColor(colorLiteralRed: 217/255, green: 83/255, blue: 79/255, alpha: 0.85)
backgroundImageView.addSubview(picker)
print(list)
}
func panGestureRecognizerAction(_ gesture: UIPanGestureRecognizer){
let translation = gesture.translation(in: view)
if let bgImage = gesture.view{
if list.count > 1{
bgImage.frame.origin.y = translation.y
}
}
if gesture.state == .ended{
let tickName = "Tick.png"
let tickImage = UIImage(named: tickName)
let tick = UIImageView(image: tickImage!)
tick.frame = CGRect(x: ((self.view.frame.width)/2)-25, y: 150, width: 70, height: 70)
self.view.addSubview(tick)
tick.alpha = 0
let crossName = "Cross.png"
let crossImage = UIImage(named: crossName)
let cross = UIImageView(image: crossImage!)
cross.frame = CGRect(x: ((self.view.frame.width)/2)-25, y: (self.view.frame.height)-75, width: 50, height: 50)
self.view.addSubview(cross)
cross.alpha = 0
let velocity = gesture.velocity(in: view)
if (gesture.view?.frame.origin.y)! > CGFloat(100) || velocity.y > 1500 {
UIView.animate(withDuration: 0.4, animations: {
self.view.backgroundColor = greenColor
gesture.view?.frame.origin = CGPoint(x: 0, y: 275)
tick.alpha = 1
}, completion: { (true) in
UIView.animate(withDuration: 0.4, delay: 0.2, animations: {
tick.alpha = 0
})
if partyAt != nil{
self.uploadPartyPost()
} else{
self.uploadPost()
}
})
UIView.animate(withDuration: 0.5, delay: 1, animations: {
gesture.view?.frame.origin = CGPoint(x: 0, y: 0)
}, completion: { (true) in
self.dismiss(animated: true, completion: nil)
})
//send image here tick
} else if (gesture.view?.frame.origin.y)! < CGFloat(-80) || velocity.y > 1500 {
UIView.animate(withDuration: 0.2, animations: {
self.view.backgroundColor = redColor
gesture.view?.frame.origin = CGPoint(x: 0, y: -150)
cross.alpha = 1
}, completion: { (true) in
UIView.animate(withDuration: 0.5, delay: 0.7, animations: {
cross.alpha = 0
})
})
UIView.animate(withDuration: 0.2, delay: 1, animations: {
gesture.view?.frame.origin = CGPoint(x: 0, y: 0)
}, completion: { (true) in
self.dismiss(animated: true, completion: nil)
})
print("Done")
//send image here cross
} else {
UIView.animate(withDuration: 0.3, animations: {
gesture.view?.frame.origin = CGPoint(x: 0, y: 0)
})
}
}
}
应该这样做:)
答案 6 :(得分:0)
当您从viewDidLoad
设置约束时,但此方法中的视图布局不正确。您应该在完成视图布局时设置约束。所以你应该在:
override func viewDidLayoutSubviews() {
// set constraints here
}
您可以使用变量来检查是否已添加约束。
答案 7 :(得分:0)
尝试以下代码,可能因为自动调整而无法更改帧,因此请尝试在viewDidAppear
方法中编写以下代码
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// Set the frame of scrollview
self.scrollViewName.frame = CGRectMake(self.scrollViewName.frame.origin.x,
self.scrollViewName.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
}
答案 8 :(得分:0)
将scrollView
固定到view
,而不是设置frame
。
添加一个约束,将contentView
的宽度设置为等于scrollView
的宽度:
contentView.widthAnchor.constraint(equalTo: scrollView.widthAnchor).isActive = true