2017-04-17 10 views
1

私のUITableViewでアラートが表示されています。 これはコードアラートビューが表示されたときのボタンの位置が変更されました

-(void)showAlertMaintenance{ 
AppDelegate * appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate]; 
[RMUniversalAlert showAlertInViewController:self withTitle:nil message:appDelegate.maintenanceStr cancelButtonTitle:@"OK" destructiveButtonTitle:nil otherButtonTitles:nil tapBlock:^(RMUniversalAlert * _Nonnull alert, NSInteger buttonIndex) { 
     if(buttonIndex == alert.cancelButtonIndex){ 

     } 
}]; 

}

ボタンは私のUITableView内の1つのセクションヘッダーのサブビューがある

UIButton *sendInviteCodeButton = [UIButton buttonWithType:UIButtonTypeCustom]; 

       [sendInviteCodeButton addTarget:self 
             action:@selector(shareInvitationCode) 
           forControlEvents:UIControlEventTouchUpInside]; 

       [sendInviteCodeButton setTitle:@"招待コードをシェアする" forState:UIControlStateNormal]; 

       [sendInviteCodeButton sizeToFit]; 
       [sendInviteCodeButton setTitleColor:[UIColor colorWithRed:215.0f/255.0f green:116.0f/255.0f blue:52.0f/255.0f alpha:1.0f] forState:UIControlStateNormal]; 
       sendInviteCodeButton.translatesAutoresizingMaskIntoConstraints = NO; 

       sendInviteCodeButton.frame = CGRectMake(20, 155.0f, SCREEN_BOUNDS_SIZE_PORTRAIT_WIDTH - 40, 30.0f); 

       [sendInviteCodeButton.layer setMasksToBounds:YES]; 
       [sendInviteCodeButton.layer setCornerRadius:5.0f]; 
       [sendInviteCodeButton.layer setBackgroundColor:[UIColor whiteColor].CGColor]; 
       [sendInviteCodeButton setBackgroundColor:[UIColor whiteColor]]; 
       [sendInviteCodeButton.titleLabel setFont:[UIFont boldSystemFontOfSize:13]]; 

       [invitationView insertSubview:sendInviteCodeButton atIndex:0]; 

Before

After

アラートがありますショーd、ボタンは画面の左上隅に押されています。

助けてください!

答えて

1

ボタンの特定のフレームを既に計算している場合は、[sendInviteCodeButton sizeToFit];を削除する必要があります。

だけ[sendInviteCodeButton sizeToFit];

はそれが助けを願って削除します。 :)

0

レイアウトを設定するためにフレームを使用しているので、あなたのテーブルビューヘッダー用の専用のUIViewサブクラスが必要です。この時点でヘッダービューには独自のフレームが正しく設定されるため、layoutSubviewsメソッドをオーバーライドする必要がありますサブビュー(ボタンを含む)の相対的な計算は理にかなっています。簡単なサンプルコードは次のとおりです。

#import <UIKit/UIKit.h> 
@interface MyTableViewHeader : UIView 
@end 

@implementation MyTableViewHeader 

-(instancetype)init 
{ 
    self = [super init]; 
    if (self) { 
     //setup button here (omit setting frames) 
    } 
    return self; 
} 

-(void)layoutSubviews 
{ 
    //set the button frame here 
    [super layoutSubviews]; 
} 
@end 

@interface ViewController : UIViewController<UITableViewDelegate> 
@property (nonatomic, strong) MyTableViewHeader *header; 
@end 

@implementation ViewController 
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
//You will probably want some logic here 
//to decide if you want to display the header 
    if (!self.header) { 
     self.header = [MyTableViewHeader new]; 
    } 
    return self.header; 
} 
@end 

また、テーブルヘッダービュー用にautolayoutを設定することもできます。

0

私はこの

[sendInviteCodeButton.layer setMasksToBounds:YES]; 

setMasksToBoundsによって引き起こされる問題を見つけた - サブ層は層の境界

にクリップされているかどうかを示すブール値です
関連する問題