开发者

Force UITableViewCell to keep objects right-aligned on orientation change

开发者 https://www.devze.com 2023-02-20 22:11 出处:网络
I have a UITableViewCell. The textLabel.text is set to some value. To the cell\'s subview, I add either a UILabel, UIButton or a UITextField (just one of these per cell). I add those views using a CGR

I have a UITableViewCell. The textLabel.text is set to some value. To the cell's subview, I add either a UILabel, UIButton or a UITextField (just one of these per cell). I add those views using a CGRectMake, which means I have to specify the X position.

But when I change the iPad's orientation, the UITableViewCell stretches and so the UILabel, UIButton or the UITextField isn't 开发者_Go百科right aligned anymore. It is a few pixels off to the left, which makes it look ugly.

How can I make the objects on the right-hand side stay right-aligned, even when the orientation changes?

(If you go to iPad's "Settings" -- see the settings on the right view -- any orientation changes, and they stay glued to the right-end of the UITableViewCell. That's the effect I'm after here.)


OK, figured it out! You can just set the UITableViewCell's accessoryView property to the control object and it just works!

cell.accessoryView = the_control

No need to mess around with anything else. I found this out by reading the Table View Programming Guide for the iOS from the iOS Developer Library, which I should've done in the first place :-)


You should set the autoresizingMask property of the to-be-glued view such that it keeps a fixed distance from the right edge. In IB this is point-and-click, in code this is done by

 myview.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号