iPad,在 viewForHeaderInSection 中调整自定义按钮的大小

Posted

技术标签:

【中文标题】iPad,在 viewForHeaderInSection 中调整自定义按钮的大小【英文标题】:iPad, Resize custom button in viewForHeaderInSection 【发布时间】:2011-10-05 09:12:04 【问题描述】:

我的应用程序具有拆分视图,并且仅在横向模式下激活。 在 masterview 我有 tableview,在最后一部分我有一个自定义按钮。 问题是按钮宽度保持在 potrait 而不是使用设备方向。 我尝试在按钮和视图上使用 setAutoresizingMask,但这也不起作用。

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger) sectionNum 
    if (sectionNum == 4) 
        CGRect frameSize;

        if (UIInterfaceOrientationIsLandscape([[UIDevice currentDevice] orientation])) 
            frameSize = CGRectMake(45.0, 0.0, 600.0, 44.0);
         else 
            frameSize = CGRectMake(45.0, 0.0, 680.0, 44.0);
        

        UIView *btnView = [[[UIView alloc] initWithFrame:frameSize] autorelease];

        // button
        UIButton* btnFind = [UIButton buttonWithType:UIButtonTypeCustom];
        [btnFind setBackgroundImage:[[UIImage imageNamed:@"buttonblue.png"] stretchableImageWithLeftCapWidth:14.0 topCapHeight:0.0] forState:UIControlStateNormal];
        [btnFind setBackgroundImage:[[UIImage imageNamed:@"buttonred.png"] stretchableImageWithLeftCapWidth:14.0 topCapHeight:0.0] forState:UIControlStateHighlighted];

        btnFind.frame = frameSize;
        btnFind.tag = 1;

        [btnFind setTitle:@"Find" forState:UIControlStateNormal];
        [btnFind addTarget:self action:@selector(doSearchDoc:) forControlEvents:UIControlEventTouchUpInside];
        //[btnFind setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin];

        //[btnView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
        [btnView addSubview:btnFind];

        return btnView;
    

    return nil;

谁能告诉我如何解决这个问题。

谢谢。

【问题讨论】:

【参考方案1】:

我还有其他问题,即旋转设备时标题未刷新,即。在我的情况下没有调用上述方法,您可以检查您是否也是这种情况。 我采用的解决方案是在旋转方法中调用 reloadData :

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
    // This solves an issue that the header doesn't show when roating device (ok on simulator)
    NSIndexPath* ipselected = [self.tableView indexPathForSelectedRow];
    [self.tableView reloadData];
    [self.tableView selectRowAtIndexPath:ipselected animated:false scrollPosition:UITableViewScrollPositionMiddle];

当然,如果您可以在旋转设备时完全重新加载,则它是可用的,但通常情况下,用户会更好地接受 0.5 秒的延迟,因为他/她正忙于移动设备。

或者,您可以在按钮中保留一个 ivar,并在上述方法中更改其框架。

【讨论】:

在我的情况下效果更好:我修改了您的代码并将 .reloadData 替换为 .reloadSections: [NSIndexSet indexSetWithIndex:btnSectionIndex] withRowAnimation:NO];

以上是关于iPad,在 viewForHeaderInSection 中调整自定义按钮的大小的主要内容,如果未能解决你的问题,请参考以下文章

如何在 iPad 硬件中测试 iPad 应用程序(不在模拟器中)

iPad 3(或新 iPad)上的 CSS 媒体查询

iPad 和 iPad 2 渲染 CSS 不同?

如何在 Cocoa 中区分 iPad 1 和 iPad 2?

图像未在 iPad Air、iPad 3 等较新的 iPad 设备上显示,但在模拟器 iOS 8.1 上显示

如何在没有 ipad 的情况下在纵向和横向条件下测试 ipad 网站?