addSubview不适用于新创建的scrollview

时间:2018-02-23 08:36:44

标签: ios swift uiscrollview uilabel addsubview

//ScrollView Creation
var scrollviewsub = UIScrollView(frame: CGRect(x: 20, y: 40, width: Int(self.view.frame.size.width)-40, height: Int(self.view.frame.size.height) - 80))
scrollviewsub.layer.cornerRadius = 20.0
scrollviewsub.layer.masksToBounds = true
scrollviewsub.backgroundColor = UIColor.blue
scrollviewsub.isHidden = false
scrollviewsub.isScrollEnabled = true
self.view.addSubview(scrollviewsub)
self.view.bringSubview(toFront: scrollviewsub)
var scrollsub = 0

//Label Creation
let dynamicLabelsub = UILabel(frame: CGRect(x: 5, y: 5, width: 50, height: 30))
dynamicLabelsub.textColor = UIColor.black
dynamicLabelsub.backgroundColor = UIColor.black
dynamicLabelsub.textAlignment = NSTextAlignment.left
dynamicLabelsub.text = "\(self.num)" + "." + "\(questno) " + subque
dynamicLabelsub.tag = uniqueid
dynamicLabelsub.isHidden = false
dynamicLabelsub.numberOfLines = 0
dynamicLabelsub.sizeToFit()

// Label Insertion 
scrollviewsub.addSubview(dynamicLabelsub)
scrollviewsub.bringSubview(toFront: dynamicLabelsub)

上面的代码显示了制作滚动视图(在屏幕上显示/显示)所涉及的过程。与scrollview不同的标签视图不会出现。知道为什么吗?

1 个答案:

答案 0 :(得分:1)

您是否在视图中添加了滚动视图?

self.view .addSubview(scrollviewsub) 并将标签颜色更改为白色或其可见的内容,视图和标签颜色相同

因为我运行你的代码并且它正常工作,还添加了scollview和标签

enter image description here