ios - How to set UITextField in UINavigationBar sizeToFit? -


i have been unable find solution issue. unable give uitextfield, textfield, sizetofit property when placed in uinavigationbar.

i have tried: textfield.sizetofit()

and of course interface builder doesn't give options when put uinavigationbar. solutions awesome, thank you!

create outlet textfield , call function in viewdidload(you paste code below directly in viewdidload):

func setwidth() {     var frame: cgrect = self.textfield.frame     frame.size.width = self.view.frame.width     self.textfield.frame = frame } 

results:

iphone 6s plus: enter image description here

iphone 6: enter image description here

iphone 5: enter image description here

update
change size on rotation add following lines of code:

in viewdidload add observer:

nsnotificationcenter.defaultcenter().addobserver(self, selector: "rotated", name: uideviceorientationdidchangenotification, object: nil) 

add function detect orientation , resize textfield:

func rotated(){     if(uideviceorientationislandscape(uidevice.currentdevice().orientation)){         print("landscape")         setwidth()     }      if(uideviceorientationisportrait(uidevice.currentdevice().orientation)){         print("portrait")         setwidth()     } } 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -