Using SizeToFit() to Top Align UILabel Causes Jumping
The following code works to vertically align text in a UILabel to the top-left of the view box. However, it causes the text to jump back and forth between proper vertical top alignment and vertical centering. The cadence of it's jumping seems to be about that of the 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 I can avoid this infernal jumping around?
UPDATE:
If you are using storyboard and auto layout you should not use the sizeToFit()
. sizeToFit()
is part of the "old" way, pre Auto Layout, and will not behave as expected if Storyboard uses any Constraints
.
Instead in Interface Builder:
This will make sure the position is correct, and allow for the Auto Layout to resize the UILabels height as needed when content is updated.
UPDATE 2:
Not entirely sure why I got down voted, as the suggested answer works. In this example, I am using a timer to update my label, see result:
链接地址: http://www.djcxy.com/p/28266.html上一篇: 如何在段落中对齐UILabel文本