Text in UITextField moves up after editing (center while editing)

前端 未结 13 1115
执念已碎
执念已碎 2020-12-01 03:14

I have a strange problem. I have an UITextField in which the user should write the amount of something, so the field is called \"amountField\". Everything looks fine, when t

13条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-01 03:57

    I used this extension. Only problem was I didn't add translatesAutoresizingMaskIntoConstraints = true before I called it. Ah silly goose

    func centerVertically() {
        textContainerInset = UIEdgeInsets.zero
        textContainer.lineFragmentPadding = 5
        let fittingSize = CGSize(width: bounds.width, height: 
         CGFloat.greatestFiniteMagnitude)
        let size = sizeThatFits(fittingSize)
        let topOffset = (bounds.size.height - size.height * zoomScale) / 2
        let positiveTopOffset = max(1, topOffset)
        contentOffset.y = -positiveTopOffset
    }
    

提交回复
热议问题