我有一個自定義表單元格,我想在左側添加一個自定義UIButton,在單元格內垂直居中。我試圖用下面顯示的代碼來做到這一點,這是我的自定義表格單元實現。如何在UITableViewCell中垂直居中對齊UIButton? (Objective C)
我試圖通過採取self.frame的高度和調整按鈕的大小來實現垂直中心對齊,使其從單元格的頂部和底部出現5px。當我運行這個時,我看到按鈕出現在頂部5px,但從底部(20px左右)顯着更多。
什麼是實現垂直對齊的正確方法?
這是我自定義表格單元格的實現代碼:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
[self setSelectionStyle:UITableViewCellSelectionStyleNone];
// Create the audio button on the left side:
self.audioButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.audioButton.frame = CGRectMake(5, 5, self.frame.size.height - 10, self.frame.size.height - 10);
[self.audioButton setImage:[UIImage imageNamed:@"ec-icon-speaker.png"] forState:UIControlStateNormal];
[self.audioButton addTarget:self
action:@selector(playWordAudio)
forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:self.audioButton];
感謝您的任何建議,
埃裏克
啊,對 - 這裏的技巧是看self.contentView.frame而不是self.frame。謝謝! –