2011-01-02 11 views
0

私はプログラムでtableViewフッターにUIButtonを追加します。このボタンはのtableViewマージンに等しく、左と右のマージンを持っています余白を維持してプログラムでUIButtonのサイズを変更する

UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 

deleteButton.frame = CGRectMake(10, 60, 300, 34); 
deleteButton.autoresizingMask = UIViewAutoresizingFlexibleWidth 

私は回転をサポートしたいので、私はautoresizingMaskを追加しています。しかし、下の図に示すように、ボタンが右にずっと下がっているので、私が望むように動作しません。

どのように修正するのですか?自動サイズ変更のプロパティを削除すると、余白が正しいことになります。

alt text

答えて

1

UITableView- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)sectionによって返さ図(そのすべてのサブビュー)をリサイズ。問題を解決するには、自動サイズ変更を使用せずにUIButtonのレイアウトを作成するラッパービューが必要です。ここでは、そのようなUIViewクラスの実装例です:

@interface ButtonWrapperView : UIView { 
    UIButton *_button; 
} 

@property (nonatomic, readonly) UIButton *button; 

@end 


@implementation ButtonWrapperView 

@synthesize button = _button; 

- (id)init 
{ 
    if ((self = [super initWithFrame:CGRectZero])) { 
     _button = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain]; 
     [self addSubview:_button]; 
    } 
    return self; 
} 

- (void)layoutSubviews 
{ 
    [super layoutSubviews]; 

    // Layout button 
    _button.frame = CGRectMake(10.0f, 0.0f, self.bounds.size.width - 20.0f, self.bounds.size.height); 
} 

- (void)dealloc 
{ 
    [_button release]; 
    [super dealloc]; 
} 

@end 

は単に- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)sectionでこのビューを返し、あなたのボタンが正しく表示されなければなりません。

また、上記の解決策を完全に実装したuploaded a sample projectもあります。

+0

本当にありがとうございます!私はこれを後で試してみるつもりです。 –

関連する問題