Добавить вид в другой вид программно - PullRequest
0 голосов
/ 26 декабря 2018

Итак, у меня есть один вид, добавленный из 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)

}

как следуетЯ реализую это?

Я не добавил вид программно, поэтому не знаю, где я ошибаюсь

Ответы [ 3 ]

0 голосов
/ 26 декабря 2018

Просто добавьте customView как подпредставление jobListBackgroundView

jobListBackgroundView.addSubview(customview)

func setUpcomingInterviewView(){
    ...
    customview.addSubview(titleLabel)
    customview.addSubview(button)
    jobListBackgroundView.addSubview(customview)
}
0 голосов
/ 26 декабря 2018

Попробуйте с кодом ниже

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)

}
0 голосов
/ 26 декабря 2018

Вам вообще не нужно использовать 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)
}
...