显示警报视图时按钮更改位置

时间:2017-04-17 08:07:58

标签: ios objective-c

我在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

当显示警告时,按钮被推到屏幕的左上角。

请帮忙!

3 个答案:

答案 0 :(得分:1)

如果您已经为按钮计算了特定框架,则应删除[sendInviteCodeButton sizeToFit];

只需删除[sendInviteCodeButton sizeToFit];

即可

希望有所帮助。 :)

答案 1 :(得分:0)

由于您依靠框架来设置布局,因此您应该为tableview标头设置专用的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

作为替代方案,您还可以为表格标题视图设置自动布局。

答案 2 :(得分:0)

我发现了由此引起的问题

[sendInviteCodeButton.layer setMasksToBounds:YES]; 

setMasksToBounds - 是一个布尔值,表示子图层是否被剪切到图层的边界