ios - Using SizeToFit() to Top Align UILabel Causes Jumping -
the following code works vertically align text in uilabel top-left of view box. however, causes text jump , forth between proper vertical top alignment , vertical centering. cadence of it's jumping seems of location update rate.
func locationmanager(manager: cllocationmanager!, didupdatelocations locations: [anyobject]!) { var userlocation: cllocation = locations[0] as! cllocation clgeocoder().reversegeocodelocation(userlocation, completionhandler: { (placemarks, error) -> void in if( error != nil ) { println("error: \(error.localizeddescription)") return } if( placemarks.count > 0 ) { let placemark = placemarks[0] as! clplacemark self.nearestaddresslabel.text = abcreatestringwithaddressdictionary(placemark.addressdictionary, false); self.nearestaddresslabel.sizetofit() } else { println("no placemarks!") } }) } gif of problem: https://imgflip.com/gif/m82jf
any idea how can avoid infernal jumping around?
update:
if using storyboard , auto layout should not use sizetofit(). sizetofit() part of "old" way, pre auto layout, , not behave expected if storyboard uses constraints.
instead in interface builder:
- set lines 0 (allow many lines)
- position label needed using constraints
- add fixed constraint width
- add contraints height using less or equal
this make sure position correct, , allow auto layout resize uilabels height needed when content updated.

update 2:
not entirely sure why got down voted, suggested answer works. in example, using timer update label, see result:




Comments
Post a Comment