Forum Archive

background color cannot be set for textfield

peter

Hi All,

In my toy program, the root view contains a text field control which was added in the UI editor. The background color of the text field is always white regardless of the color setting in UI editor. I also tried to set the color by code and it had no effect on the UI. Any idea?

Thanks.

cvp

@peter put you TextField over an ui.View and play with its opacity, like here

from objc_util import *
import ui

v = ui.View()
v.frame = (0,0,200,50)

tf_back = ui.View()
tf_back.frame = (10,10,180,30)
tf_back.background_color = 'blue'
tf_back.corner_radius = 5
v.add_subview(tf_back)

tf = ui.TextField()
tf.frame = (0,0,tf_back.width,tf_back.height)
tf.alpha = 0.9
tf_back.add_subview(tf)

v.present('sheet')
peter

@cvp thank you for the quick response.

Your method works but not ideal because the text also becomes transparent. Is this bug or intended behavior?

cvp

@peter You are right, and I think it is the normal behavior.
There should be an objectiveC solution...

Edit: I have tried some ones but without success, up to now

cvp

@peter Eureka (I had used this textField() one year ago and had already forgotten 😢)

from objc_util import *
import ui

v = ui.View()
v.frame = (0,0,200,50)

tf = ui.TextField()
tf.frame = (10,10,180,30)
tf.tint_color = 'black'
v.add_subview(tf)

tfo = ObjCInstance(tf).textField()
tfo.backgroundColor = ObjCClass('UIColor').colorWithRed_green_blue_alpha_(1, 0, 0, 1)

v.present('sheet')
peter

Your objectiveC code worked wonderfully. Thank you again.