简体   繁体   English

无法设置滚动的宽度查看iOS Swift

[英]Unable to set the width of scroll View iOS Swift

I am unable to set the width of the scrollView to the width of the screen. 我无法将scrollView的宽度设置为屏幕的宽度。 Could someone please guide where I am going wrong ? 有人可以指导我哪里出错吗?

在此输入图像描述

Here is how I have enabled a scroll view 这是我如何启用滚动视图

// 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
    ....
}

// The below functions are used to pin one view to another view //以下函数用于将一个视图固定到另一个视图

   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)
            ])
    }

Use below methods for get exact size of screen accordingly iPhone. 使用以下方法可以获得相应的iPhone屏幕尺寸。 Hope using this method, your problem solved. 希望用这种方法,你的问题解决了。 If you have doubt Please comment here. 如果您有疑问请在这里发表评论。

//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
}

viewDidLoad load your ui from storyboard or nib . viewDidLoadstoryboardnib加载你的ui。 And size depend on what you select in Interface Builder . 大小取决于您在Interface Builder选择的内容。

You can initialize ui in viewWillAppear and add a variable isInitialized . 您可以在viewWillAppear初始化ui并添加变量isInitialized

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    if !isInitialized {
        isInitialized = true
        // do your stuff
    }
}

Scroll.contentSize = CGSize(width:self.view.frame.size.width,height:self.view.frame.size.height)

将一个子视图水平居中(使用自动布局约束)到界面构建器中的滚动视图或通过编程方式

Ok so there are a couple of ways you can fix this: 好的,有几种方法可以解决这个问题:

1.- in the storyboard go to your view and you're going to hold the left click in your scroll view and release it over the main view, then you'll click equal width 1.-在故事板中转到您的视图,您将在滚动视图中按住左键并在主视图上将其释放,然后您将单击等宽

2.- in the view did load put 2.-在视图中确实加载了

self.scrollViewName.frame.size.width = self.view.frame.width

You can set the contentInset on the scrollView, and it will adjust the content size of the scrolling area within the scrollView, here is a quick example 你可以在scrollView上设置contentInset,它会调整scrollView中滚动区域的内容大小,这里有一个简单的例子

scrollView.contentInset UIEdgeInsets(top: 0, left: 0, bottom: 0, right: padding)

That should do the trick :) 这应该够了吧 :)

As you are setting constraints from viewDidLoad , but view is not properly layout in this method. 在从viewDidLoad设置约束时,但在此方法中视图布局不正确。 You should set constraint when lay out of views is done. 您应该在完成视图布局时设置约束。 So you should set constraints in : 所以你应该设置约束:

override func viewDidLayoutSubviews() {
    // set constraints here 
}

And you can use a variable to check if constraints are already added. 您可以使用变量来检查是否已添加约束。

Try following code, may be you are not able to change frame because of autoresizing, So try to write following code in viewDidAppear method 尝试下面的代码,可能是因为自动调整你无法更改帧,所以尝试在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);

}

Pin your scrollView to view instead of setting its frame . 固定scrollViewview而不是设置其frame

Add a constraint that sets the width of the contentView equal to the width of the scrollView : 添加一个约束,将contentView的宽度设置为等于scrollView的宽度:

contentView.widthAnchor.constraint(equalTo: scrollView.widthAnchor).isActive = true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM