UIButton can not display by using some specific iPhone devices models

I was trying to create a UIButton by code, and I want to add the UIButton as a subview into a UITextView.

Here is the code:

private func initTermsTextView() {


        let filePath = Bundle.main.path(forResource: "terms", ofType: "html", inDirectory: nil, forLocalization: nil) ?? ""

        let htmlString = try! String(contentsOfFile: filePath)

        let htmlData = NSString(string: htmlString).data(using: String.Encoding.unicode.rawValue)

        let options = [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html]

        let attributedString = try! NSMutableAttributedString(data: htmlData!, options: options, documentAttributes: nil)

        let font = UIFont(name: "BrandonGrotesque-Regular", size: 20) ?? UIFont.systemFont(ofSize: 20)

        attributedString.setFontFace(font: font, color: UIColor.grayMetalic())




        let btn: UIButton = UIButton(frame: CGRect(x: 8, y: termsTextView.contentSize.height*11-135, width: 48, height: 48))

        btn.setTitle("asdasdasdasda", for: .normal)

        btn.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)

        btn.clipsToBounds = true

        btn.setImage(UIImage(named: "uncheckedBox"), for: .normal)


        let path = UIBezierPath(rect: CGRect(x: 8, y: termsTextView.contentSize.height*11-135, width: btn.frame.width, height: btn.frame.height))

        self.termsTextView.textContainer.exclusionPaths = [path]


        self.termsTextView.attributedText = attributedString






Since I have no idea how to add UIButton at the bottom of a UITextView, I did hardcore and count out the UITextView length(text length*11-135) and added the button under the UITextView.

After I was running my code with devices which is above the iPhone 6 (include 6s and plus) and the UIButton has displayed in the UITextView. But when I tried with iPhone 6 plus and iPhone 6s plus, the UIButton sub view did not show in the UITextView and I have no idea why.

Is there any way I can fix this type of problem?

Powered by WPeMatico

You may also like...

Comments are closed.