旋转时自动调整 UITableView 标题大小(主要在 iPad 上)

发布于 2024-12-10 23:47:14 字数 1181 浏览 0 评论 0原文

我觉得这将是一个围绕 AutoResizingMasks 的简单答案,但我似乎无法理解这个主题。

我有一个 iPad 应用程序,可以并排显示 2 个 UITableView。当我从纵向旋转到横向并返回时,UITableView 中的单元格会在旋转发生时即时完美地调整大小。我正在使用 UITableViewCellStyleSubtitle UITableViewCells (目前未子类化),并且我已在 IB 中设置 UITableView 以锚定到顶部、左侧和底部边缘(对于左侧 UITableView)并具有灵活的宽度。

我正在提供我自己的 UIView 对象,

- (UIView *)tableView:(UITableView *)tableView 
     viewForHeaderInSection:(NSInteger)section

这就是我到目前为止所得到的(称为另一个类的类方法):

+ (UIView *)headerForTableView:(UITableView *)tv
{
    // The view to return 
    UIView *headerView = [[UIView alloc] 
        initWithFrame:CGRectMake(0, 0, [tv frame].size.width, someHeight)];

    [headerView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | 
                                    UIViewAutoresizingFlexibleLeftMargin | 
                                    UIViewAutoresizingFlexibleRightMargin];

    // Other layout logic... doesn't seem to be the culprit

    // Return the HeaderView
    return headerView;
}

因此,无论方向如何,所有内容都会按照我想要的方式加载。旋转后,如果我手动调用 reloadData 或等待我的应用程序触发它,或滚动 UITableView,headerView 将调整大小并正确显示自己。我不知道如何正确设置 AutoResizeMask 属性,以便标题像单元格一样调整大小。

I feel like this is going to be a simple answer revolving around AutoResizingMasks, but I can't seem to wrap my head around this topic.

I've got an iPad app that shows 2 UITableViews side-by-side. When I rotate from Portrait to Landscape and back, the cells in the UITableView resize perfectly, on-the-fly, while the rotation is occurring. I'm using UITableViewCellStyleSubtitle UITableViewCells (not subclassed for now), and I've set the UITableView up in IB to anchor to the top, left and bottom edges (for the left UITableView) and to have a flexible width.

I'm supplying my own UIView object for

- (UIView *)tableView:(UITableView *)tableView 
     viewForHeaderInSection:(NSInteger)section

Here's what I've got so far (called as a class method from another class):

+ (UIView *)headerForTableView:(UITableView *)tv
{
    // The view to return 
    UIView *headerView = [[UIView alloc] 
        initWithFrame:CGRectMake(0, 0, [tv frame].size.width, someHeight)];

    [headerView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | 
                                    UIViewAutoresizingFlexibleLeftMargin | 
                                    UIViewAutoresizingFlexibleRightMargin];

    // Other layout logic... doesn't seem to be the culprit

    // Return the HeaderView
    return headerView;
}

So, in either orientation, everything loads up just like I want. After rotation, if I manually call reloadData or wait until my app triggers it, or scroll the UITableView, the headerViews will resize and show themselves properly. What I can't figure out is how to get the AutoResizeMask property set properly so that the header will resize just like the cells.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

━╋う一瞬間旳綻放 2024-12-17 23:47:14

不是一个很好的修复。但有效:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
    [mTableView reloadData];
}

Not a very good fix. But works :

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
    [mTableView reloadData];
}
笑红尘 2024-12-17 23:47:14

我最近遇到了同样的问题。诀窍是使用自定义视图作为表的 headerView。重写layoutSubviews允许我随意控制布局。下面是一个例子。

#import "TableSectionHeader.h"

@implementation TableSectionHeader

- (id)initWithFrame:(CGRect)frame title:(NSString *)title
{
    self = [super initWithFrame:frame];
    if (self) {

        self.backgroundColor = [UIColor clearColor];

        // Initialization code
        headerLabel = [[UILabel alloc] initWithFrame:frame];
        headerLabel.text = title;

        headerLabel.textColor = [UIColor blackColor];
        headerLabel.font = [UIFont boldSystemFontOfSize:17];
        headerLabel.backgroundColor = [UIColor clearColor];

        [self addSubview:headerLabel];
    }
    return self;
}

-(void)dealloc {

    [headerLabel release];

    [super dealloc];
}

-(void)layoutSubviews {

    [super layoutSubviews];

    NSInteger xOffset = ((55.0f / 768.0f) * self.bounds.size.width);

    if (xOffset > 55.0f) {
        xOffset = 55.0f;
    }

    headerLabel.frame = CGRectMake(xOffset, 15, self.bounds.size.width - xOffset * 2, 20);
}

+(UIView *) tableSectionHeaderWithText:(NSString *) text bounds:(CGRect)bounds {
    TableSectionHeader *header = [[[TableSectionHeader alloc] initWithFrame:CGRectMake(0, 0, bounds.size.width, 40) title:text] autorelease];
    return header;
}

+(CGFloat) tableSectionHeaderHeight {
    return 40.0;
}
@end

I faced the same issue recently. The trick was to use a custom view as the headerView of the table. Overriding layoutSubviews allowed me to control the layout at will. Below is an example.

#import "TableSectionHeader.h"

@implementation TableSectionHeader

- (id)initWithFrame:(CGRect)frame title:(NSString *)title
{
    self = [super initWithFrame:frame];
    if (self) {

        self.backgroundColor = [UIColor clearColor];

        // Initialization code
        headerLabel = [[UILabel alloc] initWithFrame:frame];
        headerLabel.text = title;

        headerLabel.textColor = [UIColor blackColor];
        headerLabel.font = [UIFont boldSystemFontOfSize:17];
        headerLabel.backgroundColor = [UIColor clearColor];

        [self addSubview:headerLabel];
    }
    return self;
}

-(void)dealloc {

    [headerLabel release];

    [super dealloc];
}

-(void)layoutSubviews {

    [super layoutSubviews];

    NSInteger xOffset = ((55.0f / 768.0f) * self.bounds.size.width);

    if (xOffset > 55.0f) {
        xOffset = 55.0f;
    }

    headerLabel.frame = CGRectMake(xOffset, 15, self.bounds.size.width - xOffset * 2, 20);
}

+(UIView *) tableSectionHeaderWithText:(NSString *) text bounds:(CGRect)bounds {
    TableSectionHeader *header = [[[TableSectionHeader alloc] initWithFrame:CGRectMake(0, 0, bounds.size.width, 40) title:text] autorelease];
    return header;
}

+(CGFloat) tableSectionHeaderHeight {
    return 40.0;
}
@end
錯遇了你 2024-12-17 23:47:14

我很想得到这个问题的真正答案,但现在,我刚刚重新设计了 UITableView,以便我的“标题”只是表格内的单元格。这样调整大小就没有问题了。

I'd love to get a real answer to this, but for now, I've just re-worked my UITableView so that my "headers" are just cells inside the table. Resizing has no issues that way.

迷你仙 2024-12-17 23:47:14

我创建了 UIView 子类,在其中使用视觉约束将子视图粘贴到屏幕的两侧。旋转一切都很好。

class MLFlexibleView: UIView {

override init(frame: CGRect) {
    super.init(frame: frame)

    self.addSubview(self.segmentedControl)
    self.setUpConstraints()
}

func setUpConstraints() {
    let views = ["view" : self.segmentedControl]

    NSLayoutConstraint.activate(NSLayoutConstraint.constraints(withVisualFormat: "V:|-7-[view]-7-|", options: [], metrics: nil, views: views))
    NSLayoutConstraint.activate(NSLayoutConstraint.constraints(withVisualFormat: "H:|-15-[view]-15-|", options: [], metrics: nil, views: views))
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

let segmentedControl:UISegmentedControl = {
    let segmentedControl = UISegmentedControl(items: ["Searches".localized, "Adverts".localized])
    segmentedControl.selectedSegmentIndex = 0
    segmentedControl.tintColor = UIColor.white
    segmentedControl.translatesAutoresizingMaskIntoConstraints = false
    return segmentedControl
}()

}

I have created UIView subclass where I've used visual constraints to stick the subview to the sides of the screen. And rotation is all fine.

class MLFlexibleView: UIView {

override init(frame: CGRect) {
    super.init(frame: frame)

    self.addSubview(self.segmentedControl)
    self.setUpConstraints()
}

func setUpConstraints() {
    let views = ["view" : self.segmentedControl]

    NSLayoutConstraint.activate(NSLayoutConstraint.constraints(withVisualFormat: "V:|-7-[view]-7-|", options: [], metrics: nil, views: views))
    NSLayoutConstraint.activate(NSLayoutConstraint.constraints(withVisualFormat: "H:|-15-[view]-15-|", options: [], metrics: nil, views: views))
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

let segmentedControl:UISegmentedControl = {
    let segmentedControl = UISegmentedControl(items: ["Searches".localized, "Adverts".localized])
    segmentedControl.selectedSegmentIndex = 0
    segmentedControl.tintColor = UIColor.white
    segmentedControl.translatesAutoresizingMaskIntoConstraints = false
    return segmentedControl
}()

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文