swift
1import UIKit 2 3class ViewController: UIViewController { 4var questionView = UITextView() 5var button:UIButton? 6 7override func viewDidLoad() { 8super.viewDidLoad() 9// Do any additional setup after loading the view. 10 11questionView = setUpquestion(contents: "こんにちはわわわわわわ") 12button = setUpChoices(c1: "押してね!") 13} 14func setUpquestion(contents:String) -> UITextView{ 15let question = UITextView() 16question.isEditable = false 17question.isSelectable = false 18question.font = UIFont.systemFont(ofSize: 23) 19question.textColor = .lightGray 20question.text = contents 21question.sizeToFit() 22question.center = self.view.center 23self.view.addSubview(question) 24 25return question 26} 27 28func setUpChoices(c1:String) -> UIButton{ 29let button1 = UIButton() 30button1.setTitle(c1, for: .normal) 31button1.sizeToFit() 32button1.setTitleColor(.systemBlue, for: .normal) 33button1.center = CGPoint(x: self.view.bounds.width / 2, y: self.view.bounds.height * 3 / 4) 34button1.tag = 0 35button1.addTarget(self, action: #selector(buttonAction(_:)), for: .touchUpInside) 36self.view.addSubview(button1) 37 38return button1 39} 40 41@objc func buttonAction(_ sender:UIButton){ 42questionView.text = "こんにちは" 43 44questionView.sizeToFit() 45 46} 47}
これの場合なぜ一番最初の"こんにちはわわわわわわ"のいちは真ん中にあるのに、後から変更した"こんにちは"では位置が真ん中に引き継がれないのでしょうか?
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。