我想在 UITableView 的标题部分显示一个 UIButton。这是我尝试过的代码
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0, 285, 44)];
UIButton *headerViewButton = [UIButton buttonWithType:UIButtonTypeCustom];
headerViewButton.frame = CGRectMake(headerView.bounds.size.width * 7/5, headerView.bounds.size.height * 1/4, 320.0, 30.0);
headerViewButton.backgroundColor = [UIColor grayColor];
[headerViewButton setTitle:@"Import main list from other field >" forState:UIControlStateNormal];
[headerViewButton addTarget:self
action:@selector(buttonPressed:)
forControlEvents:UIControlEventTouchDown];
[headerView addSubview:headerViewButton];
问题是我很难调整按钮框架,以便它适合横向模式和纵向模式。我对使用 Interface Builder 不太满意。如何正确调整按钮框架以使其适合视图。
我试过使用setAutoresizingMask
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0, 320, 44)];
UIButton *headerViewButton = [UIButton buttonWithType:UIButtonTypeCustom];
headerViewButton.frame = CGRectMake(headerView.bounds.size.width * 7/5, headerView.bounds.size.height * 1/4, 290, 30.0);
headerViewButton.backgroundColor = [UIColor grayColor];
[headerViewButton setTitle:@"Import main list from other field >" forState:UIControlStateNormal];
[headerViewButton addTarget:self
action:@selector(buttonPressed:)
forControlEvents:UIControlEventTouchDown];
[headerViewButton setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleWidth];
[headerView addSubview:headerViewButton];
当应用程序第一次加载时,按钮丢失了,但是当我改变方向时,按钮出现了。每次我尝试,它的模式都是一样的。应用程序第一次加载时,按钮会丢失,之后它会出现在两个方向上。