Ios 如何将内容宽度设置为设备/父级宽度的大小?

Ios 如何将内容宽度设置为设备/父级宽度的大小?,ios,swift,uiscrollview,viewdidlayoutsubviews,Ios,Swift,Uiscrollview,Viewdidlayoutsubviews,我的功能卷轴中的图像在较小尺寸的手机中显示得很奇怪,比如iphone5s,iphone6,等等 编辑:我正在使用UIScrollView执行此操作。:) 我已尝试将代码添加到viewdilayoutsubviews和viewWillLayoutSubviews中,但它似乎不起作用 //第一次尝试 override func viewDidLayoutSubviews() { featureScroll.contentSize = CGSize(width: self.view.bound

我的功能卷轴中的图像在较小尺寸的手机中显示得很奇怪,比如
iphone5s
iphone6
,等等

编辑:我正在使用
UIScrollView
执行此操作。:)

我已尝试将代码添加到
viewdilayoutsubviews
viewWillLayoutSubviews
中,但它似乎不起作用

//第一次尝试

override func viewDidLayoutSubviews() {
    featureScroll.contentSize = CGSize(width: self.view.bounds.width * CGFloat(featureArray.count), height: 300)
}
//第二次尝试

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()()
    featureScroll.contentSize = CGSize(width: self.view.bounds.width * CGFloat(featureArray.count), height: 300)
}
编辑: //如何将图像加载到featureScroll中

let feature1 = ["image": "ic_home1"]
let feature2 = ["image": "ic_home2"]
let feature3 = ["image": "ic_home3"]

var featureArray = [Dictionary<String, String>](


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    featureArray = [feature1, feature2, feature3]
    featureScroll.isPagingEnabled = true
    featureScroll.contentSize = CGSize(width: self.view.bounds.width * CGFloat(featureArray.count), height: 300)
    featureScroll.showsHorizontalScrollIndicator = false

    featureScroll.delegate = self

    loadFeatures()


}

func loadFeatures(){
    for (index, feature) in featureArray.enumerated() {
        if let featureView = Bundle.main.loadNibNamed("Feature", owner: self, options: nil)?.first as? FeatureView {

            featureView.featureImage.image = UIImage(named: feature["image"]!)

            featureScroll.addSubview(featureView)

            featureView.frame.size.width = featureScroll.bounds.size.width
            featureView.frame.origin.x = CGFloat(index) * featureScroll.bounds.size.width


        }
    }

}


func scrollViewDidScroll(_ featureScroll: UIScrollView) {
    let page = featureScroll.contentOffset.x / featureScroll.frame.size.width
    featurePageControl.currentPage = Int(page)
}
let feature1=[“image”:“ic_home1”]
let feature2=[“image”:“ic_home2”]
let feature3=[“image”:“ic_home3”]
var featureArray=[字典](
重写func viewDidLoad(){
super.viewDidLoad()
//加载视图后,通常从nib执行任何其他设置。
featureArray=[feature1、feature2、feature3]
featureScroll.isPaginEnabled=true
featureScroll.contentSize=CGSize(宽度:self.view.bounds.width*CGFloat(featureArray.count),高度:300)
featureScroll.showsHorizontalScrollIndicator=false
featureScroll.delegate=self
loadFeatures()
}
func loadFeatures(){
对于featureArray.enumerated()中的(索引、特征){
如果让featureView=Bundle.main.loadNibNamed(“功能”,所有者:self,选项:nil)?。首先作为?featureView{
featureView.featureImage.image=UIImage(名称:feature[“image”]!)
featureScroll.addSubview(featureView)
featureView.frame.size.width=featureScroll.bounds.size.width
featureView.frame.origin.x=CGFloat(索引)*featureScroll.bounds.size.width
}
}
}
func scrollViewDidScroll(uFeatureScroll:UIScrollView){
让page=featureScroll.contentOffset.x/featureScroll.frame.size.width
featurePageControl.currentPage=Int(第页)
}
下面的链接显示了实际的结果,我想去掉空白,我应该怎么做

-->

提前感谢您的帮助!:)

理论 scrollView需要知道它的位置(它的框架=位置和大小)和内容的大小(将滚动)

垂直堆叠视图需要知道其位置(x,y)和宽度。其高度由其内容和设置(如项目之间的间距)决定


例子 因此,根据您的需要,要在整个屏幕上显示一个滚动视图,并在其中显示一组垂直视图,您可以执行类似的操作(只有在有约束的情况下,才可以通过编程方式设置任何内容)


UIScrollView: 0到其superview的顶部、前导、尾部和底部约束


垂直视图: 0处的前导、尾随约束到ViewController的视图。顶部、前导、尾随、底部=0到其superview(scrollView)>这将使scrollView。。。stackView内容大于scrollView高度时滚动


StackView的项目: 在你的情况下,我认为这是一堆图像视图。每个项目都需要设置自己的高度

ImageView高度的总和(以及stackView中设置的间距)将决定stackView的高度,而stackView的高度将决定scrollView的内容高度(从而决定可滚动高度)

有关UIScrollView的详细信息


有关详细信息,请参见“视图”

是否所有内容都使用自动布局?第二个也是愚蠢的问题,但你在用IB吗?第三个(不那么愚蠢的)问题,您使用的是
UICollectionView
还是
UIScrollView
?总之,这些对于处理“内在”价值意义重大,我相信你会遇到这些问题。1)是的,一切都是在自动布局中完成的。2) 是的,我使用的是IB.3)我使用的是
UIScrollView
:)如果您使用的是Autolayout,能否在界面生成器中显示约束?需要更多关于如何在
UIScrollView
中添加内容的代码。尝试使用
UICollectionView
而不是
UIScrollView
@RomyIlano:以下是我使用的约束条件:)-->