2017-04-17 45 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一個子視圖

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子類爲你的tableview頭。你需要重寫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 

作爲替代方案,您還可以爲表格標題視圖設置自動佈局。

0

我發現由此引起

[sendInviteCodeButton.layer setMasksToBounds:YES]; 

setMasksToBounds問題 - 是一個布爾值,表示是否子層被剪切到圖層邊界