所以我从Storyboad添加了一个视图
@IBOutlet weak var jobListBackgroundView: UIView!
现在在一项功能中,我想以编程方式添加一个具有按钮和标签的视图。
func setUpcomingInterviewView(){
var customview = UIView()
customview.backgroundColor = UIColor.dashboardScreenHeader
customview.frame = CGRect(x: 0, y: 0, width: 375, height: 47)
let titleLabel = UILabel()
titleLabel.frame = CGRect(x: 11, y: 12, width:170, height: 18)
titleLabel.text = headerSection[0]
titleLabel.font = titleLabel.font.withSize(16)
let button = UIButton(type: .system)
button.setTitle("See All", for: .normal)
button.setTitleColor(UIColor.customBlueColor, for: .normal)
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
button.frame = CGRect(x: 303, y: 14, width: 62, height: 12)
customview.addSubview(titleLabel)
customview.addSubview(button)
customview = jobListBackgroundView(frame: CGRect(x: 0, y: 0, width: 375, height: 47))
self.view.addSubview(customview)
}
我应该如何实现呢?
我以前没有以编程方式添加视图,所以不知道我要去哪里
答案 0 :(得分:2)
尝试以下代码
func setUpcomingInterviewView(){
var customview = UIView()
customview.backgroundColor = UIColor.dashboardScreenHeader
customview.frame = CGRect(x: 0, y: 0, width: 375, height: 47)
let titleLabel = UILabel()
titleLabel.frame = CGRect(x: 11, y: 12, width:170, height: 18)
titleLabel.text = headerSection[0]
titleLabel.font = titleLabel.font.withSize(16)
let button = UIButton(type: .system)
button.setTitle("See All", for: .normal)
button.setTitleColor(UIColor.customBlueColor, for: .normal)
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
button.frame = CGRect(x: 303, y: 14, width: 62, height: 12)
customview.addSubview(titleLabel)
customview.addSubview(button)
self.jobListBackgroundView.addSubview(customview)
}
答案 1 :(得分:0)
您根本不需要使用customView
,只需像这样直接使用jobListBackgroundView
:
func setUpcomingInterviewView(){
jobListBackgroundView.backgroundColor = UIColor.dashboardScreenHeader
jobListBackgroundView.frame = CGRect(x: 0, y: 0, width: 375, height: 47)
let titleLabel = UILabel()
titleLabel.frame = CGRect(x: 11, y: 12, width:170, height: 18)
titleLabel.text = headerSection[0]
titleLabel.font = titleLabel.font.withSize(16)
let button = UIButton(type: .system)
button.setTitle("See All", for: .normal)
button.setTitleColor(UIColor.customBlueColor, for: .normal)
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
button.frame = CGRect(x: 303, y: 14, width: 62, height: 12)
jobListBackgroundView.addSubview(titleLabel)
jobListBackgroundView.addSubview(button)
}
答案 2 :(得分:0)
只需添加customView
作为jobListBackgroundView
的子视图
jobListBackgroundView.addSubview(customview)
func setUpcomingInterviewView(){
...
customview.addSubview(titleLabel)
customview.addSubview(button)
jobListBackgroundView.addSubview(customview)
}