programing

테이블 크기 조정 방법UI 테이블 뷰의 머리글 뷰?

lastcode 2023. 6. 1. 22:46
반응형

테이블 크기 조정 방법UI 테이블 뷰의 머리글 뷰?

테이블 크기를 조정하는 데 문제가 있습니다.머리글 보기.그것은 간단하지 않습니다.

UITableView 및 UIView 생성(100 x 320px);

UIView 테이블 설정UI 테이블 뷰의 머리글 뷰;

빌드 및 이동.모든 것이 괜찮습니다.

이제 테이블 크기를 조정합니다.HeaderView. 그래서 이 코드를 viewDidLoad에 추가합니다.

self.tableView.autoresizesSubviews = YES;

self.tableView.tableHeaderView = myHeaderView;
self.tableView.tableFooterView = myFooterView;

CGRect newFrame = self.tableView.tableHeaderView.frame;
newFrame.size.height = newFrame.size.height + 100;
self.tableView.tableHeaderView.frame = newFrame;

테이블 높이HeaderView는 200으로 표시되어야 하지만 100으로 표시됩니다.

내가 쓴다면:

self.tableView.autoresizesSubviews = YES;


CGRect newFrame = myHeaderView.frame;
newFrame.size.height = newFrame.size.height + 100;
myHeaderView.frame = newFrame;


self.tableView.tableHeaderView = myHeaderView;
self.tableView.tableFooterView = myFooterView;

그럼 제가 원하는 높이로 200부터 시작합니다.하지만 저는 런타임에 수정할 수 있기를 원합니다.

저는 또한 이것을 시도했지만, 성공하지 못했습니다.

self.tableView.autoresizesSubviews = YES;

self.tableView.tableHeaderView = myHeaderView;
self.tableView.tableFooterView = myFooterView;

CGRect newFrame = self.tableView.tableHeaderView.frame;
newFrame.size.height = newFrame.size.height + 100;
self.tableView.tableHeaderView.frame = newFrame;

[self.tableView.tableHeaderView setNeedsLayout];
[self.tableView.tableHeaderView setNeedsDisplay];
[self.tableView setNeedsLayout];
[self.tableView setNeedsDisplay];

요점은 다음과 같습니다.테이블 크기를 조정하는 방법런타임의 머리글 보기 ?

누가 이것을 할 수 있습니까?

감사해요.

아이미

참고로 테이블을 수정하여 이 작업을 수행했습니다.HeaderView(헤더 보기) 및 재설정.이 경우, 저는 테이블의 크기를 조정하고 있습니다.UI WebView 하위 보기 로드가 완료되었을 때의 HeaderView입니다.

[webView sizeToFit];
CGRect newFrame = headerView.frame;
newFrame.size.height = newFrame.size.height + webView.frame.size.height;
headerView.frame = newFrame;
[self.tableView setTableHeaderView:headerView];

이 답변은 오래된 것으로 iOS 7 이상에서는 작동하지 않습니다.

저는 같은 문제에 부딪혔고 또한 애니메이션으로 변경하기를 원했기 때문에 머리글 보기에 대한 UIView의 하위 클래스를 만들고 다음 방법을 추가했습니다.

- (void)adjustTableHeaderHeight:(NSUInteger)newHeight{
    NSUInteger oldHeight = self.frame.size.height;
    NSInteger originChange = oldHeight - newHeight;

    [UIView beginAnimations:nil context:nil];

    [UIView setAnimationDuration:1.0f];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];

    self.frame = CGRectMake(self.frame.origin.x, 
                        self.frame.origin.y, 
                        self.frame.size.width, 
                        newHeight);

    for (UIView *view in [(UITableView *)self.superview subviews]) {
        if ([view isKindOfClass:[self class]]) {
            continue;
        }
        view.frame = CGRectMake(view.frame.origin.x, 
                            view.frame.origin.y - originChange, 
                            view.frame.size.width, 
                            view.frame.size.height);
    }

    [UIView commitAnimations];
}

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context{
    [(UITableView *)self.superview setTableHeaderView:self];
}

기본적으로 호출 클래스와 동일한 클래스 유형이 아닌 UITableView의 모든 하위 뷰를 애니메이션으로 만듭니다.애니메이션이 끝나면 SuperView(UITableView)에서 setTableHeaderView를 호출합니다. 그렇지 않으면 다음에 사용자가 스크롤할 때 UITableView 콘텐츠가 뒤로 이동합니다.지금까지 발견한 유일한 제한 사항은 애니메이션이 진행되는 동안 사용자가 UITableView를 스크롤하려고 하면 스크롤이 헤더 보기의 크기가 조정되지 않은 것처럼 애니메이션이 생성된다는 것입니다(애니메이션이 빠르다면 큰 문제가 되지 않습니다).

조건부로 변경 사항을 애니메이션화하려면 다음을 수행합니다.

- (void) showHeader:(BOOL)show animated:(BOOL)animated{

    CGRect closedFrame = CGRectMake(0, 0, self.view.frame.size.width, 0);
    CGRect newFrame = show?self.initialFrame:closedFrame;

    if(animated){
        // The UIView animation block handles the animation of our header view
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.3];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

        // beginUpdates and endUpdates trigger the animation of our cells
        [self.tableView beginUpdates];
    }

    self.headerView.frame = newFrame;
    [self.tableView setTableHeaderView:self.headerView];

    if(animated){
        [self.tableView endUpdates];
        [UIView commitAnimations];
    }
}

애니메이션은 두 개로 구성되어 있습니다.

  1. 아래 셀의 tableHeaderView이 작업은 다음을 사용하여 수행됩니다.beginUpdates그리고.endUpdates
  2. 실제 머리글 보기의 애니메이션입니다.이 작업은 다음을 사용하여 수행됩니다.UIView애니메이션 블록

두을 동기화하기 이두애이동하기위해화기을션메니▁in위해기ations▁order▁the.animationCurve되어 있어야 .UIViewAnimationCurveEaseInOut그리고 까지 지속 시간.0.3UITableView에서 애니메이션에 사용하는 것으로 보입니다.

갱신하다

저는 Gihub에 Xcode 프로젝트를 만들었는데, 이것은 이것을 합니다.ResizeTableHeaderViewAnimated베시/아이오스 속사포로

스크린샷

내 HeaderView의 높이를 다음과 같이 설정하면 작동할 것 같습니다.

CGRect newFrame = myHeaderView.frame;
newFrame.size.height = newFrame.size.height + 100;
myHeaderView.frame = newFrame;

self.tableView.tableHeaderView = myHeaderView;

7i까지 의 @사용OS 7까지 위의 @garrettmoon 솔루션을 사용했습니다.
다음은 @garrettmoon을 기반으로 한 업데이트된 솔루션입니다.

- (void)adjustTableHeaderHeight:(NSUInteger)newHeight animated:(BOOL)animated {

    [UIView beginAnimations:nil context:nil];

    [UIView setAnimationDuration:[CATransaction animationDuration]];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];

    self.frame = CGRectMake(self.frame.origin.x,
                        self.frame.origin.y,
                        self.frame.size.width,
                        newHeight);

    [(UITableView *)self.superview setTableHeaderView:self];

    [UIView commitAnimations];
}

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context{
    [(UITableView *)self.superview setTableHeaderView:self];
}

이것은 iOS 7과 8에서 제게 효과가 있었습니다.이 코드는 테이블 뷰 컨트롤러에서 실행 중입니다.

[UIView animateWithDuration:0.3 animations:^{
    CGRect oldFrame = self.headerView.frame;
    self.headerView.frame = CGRectMake(oldFrame.origin.x, oldFrame.origin.y, oldFrame.size.width, newHeight);
    [self.tableView setTableHeaderView:self.headerView];
}];

테이블의 세터이기 때문입니다.머리글 보기.

테이블을 설정하기 전에 UIView 높이를 설정해야 합니다.HeaderView. (애플이 이 프레임워크를 오픈 소스로 제공한다면 훨씬 쉬울 것입니다...)

9 버전에서는 iOS 9를 사용할 수 있습니다.tableHeaderView크기를 조정한 후 다시 시작하지 않습니다.이 문제는 iOS 10에서 해결되었습니다.

이 문제를 해결하려면 다음 코드로 수행하십시오.

self.tableView.tableHeaderView = self.tableView.tableHeaderView;

9 9.x에서 이 수행viewDidLoad합니다.

var frame = headerView.frame
frame.size.height = 11  // New size
headerView.frame = frame

headerView는 로선됩다로 됩니다.@IBOutlet var headerView: UIView!맨을 합니다.테이블의 기능을 하기 위해 보기머리글 보기.

이것은 자동 레이아웃을 사용하고 설정하는 경우에만 해당됩니다.translatesAutoresizingMaskIntoConstraints = false사용자 정의 머리글 보기로 이동합니다.

가장 좋고 간단한 방법은 오버라이드입니다.intrinsicContentSize으로 내적로으부▁internUITableView사용하다intrinsicContentSize머리글/머리글 크기를 결정합니다.일단 오버라이드를 실행하면intrinsicContentSize해야 할 와 같습니다.

  1. 사용자 정의 머리글/키워드 뷰의 레이아웃 구성(서브뷰)
  2. 동발을 invalidateIntrinsicContentSize()
  3. 동발을 tableView.setNeedsLayout()그리고.tableView.layoutIfNeeded()

에 그면러그.UITableView의 헤더/메시지가 원하는 대로 업데이트됩니다.보기를 0으로 설정하거나 재설정할 필요가 없습니다.

한 가지 정말 흥미로운 것이 있습니다.UITableView.tableHeaderView또는.tableFooterView은 것은입니다.UIStackView그것의 관리 능력을 약화시킵니다.arrangedSubviews 당신이 경우용을 사용하고 .UIStackView tableFooterView로서 headerView tableFooterView, stackView.UIView 및오라드이버드를 재정의합니다.UIViewintrinsicContentSize.

swift 5 테스트된 코드의 경우

override func viewDidLayoutSubviews() {
      super.viewDidLayoutSubviews()

        guard let headerView = self.tblProfile.tableHeaderView else {
            return
        }

        let size = headerView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)

        if headerView.frame.size.height != size.height {
            headerView.frame.size.height = size.height
            self.tblProfile.tableHeaderView = headerView
            self.tblProfile.layoutIfNeeded()
        }
    }

참고: 모든 하위 뷰의 제약 조건을 상단, 하단, 선행, 후행으로 지정해야 합니다.그래서 그것은 전체 필요한 크기가 될 것입니다.

참조 자료: https://useyourloaf.com/blog/variable-height-table-view-header/

헤더 뷰 특성의 높이 설정tableView.tableHeaderViewviewDidLoad작동하지 않는 것 같습니다. 헤더 뷰 높이가 여전히 예상대로 변경되지 않습니다.

이 문제와 여러 번 싸워 본 후에.를 안에서 헤더 뷰 생성 할 수 알게 .- (void)didMoveToParentViewController:(UIViewController *)parent방법.

예제 코드는 다음과 같습니다.

- (void)didMoveToParentViewController:(UIViewController *)parent {
    [super didMoveToParentViewController:parent];

    if ( _tableView.tableHeaderView == nil ) {
        UIView *header = [[[UINib nibWithNibName:@"your header view" bundle:nil] instantiateWithOwner:self options:nil] firstObject];

        header.frame = CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds), HeaderViewHeight);

        [_tableView setTableHeaderView:header];
    }
}

자동 레이아웃을 사용하여 사용자 정의 헤더 뷰를 설계한 경우 웹 가져오기 또는 유사한 작업 후 헤더 뷰를 업데이트해야 합니다.그리고 iOS-Swift에서 나는 이것을 했고 아래 코드를 사용하여 내 헤더 뷰를 업데이트했습니다:

//to reload your cell data
self.tableView.reloadData()
DispatchQueue.main.async {
// this is needed to update a specific tableview's headerview layout on main queue otherwise it's won't update perfectly cause reloaddata() is called
  self.tableView.beginUpdates()
  self.tableView.endUpdates()
}

나는 그것을 찾았습니다.UIView의 WithFrame 이니셜라이저가 전달하는 rect를 제대로 준수하지 않습니다.따라서 완벽하게 작동하는 다음과 같은 작업을 수행했습니다.

 - (id)initWithFrame:(CGRect)aRect {

    CGRect frame = [[UIScreen mainScreen] applicationFrame];

    if ((self = [super initWithFrame:CGRectZero])) {

        // Ugly initialization behavior - initWithFrame will not properly honor the frame we pass
        self.frame = CGRectMake(0, 0, frame.size.width, 200);

        // ...
    }
}

이 기능의 장점은 보기 코드에 더 잘 캡슐화된다는 것입니다.

탭하면 전체 화면으로 확대되도록 표 머리글의 애니메이션 높이 변경을 구현했습니다.그러나 코드는 다른 경우에 도움이 될 수 있습니다.

// Swift
@IBAction func tapped(sender: UITapGestureRecognizer) {

    self.tableView.beginUpdates()       // Required to update cells. 

    // Collapse table header to original height
    if isHeaderExpandedToFullScreen {   

        UIView.animateWithDuration(0.5, animations: { () -> Void in
            self.scrollView.frame.size.height = 110   // original height in my case is 110
        })

    }
    // Expand table header to overall screen            
    else {      
        let screenSize = self.view.frame           // "screen" size

        UIView.animateWithDuration(0.5, animations: { () -> Void in
            self.scrollView.frame.size.height = screenSize.height
        })
    }

    self.tableView.endUpdates()  // Required to update cells. 

    isHeaderExpandedToFullScreen= !isHeaderExpandedToFullScreen  // Toggle
}

UITableView 헤더 크기 조정 - 스코프 막대가 있는 UI 검색 막대

갖고 싶었어요.UITableView와 함께UISearchBar표의 머리글로서 나는 이렇게 보이는 계층 구조를 가지고 있습니다.

UITableView
  |
  |--> UIView
  |     |--> UISearchBar
  |
  |--> UITableViewCells

IMT2000 3GPP - UI 검색바 위임 방법

다른 곳에서 언급한 바와 같이 TableView를 설정하지 않은 경우변경 후 헤더는 아무 일도 일어나지 않습니다.

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    searchBar.showsScopeBar = YES;
    [UIView animateWithDuration:0.2f animations:^{
        [searchBar sizeToFit];
        CGFloat height = CGRectGetHeight(searchBar.frame);

        CGRect frame = self.tableView.tableHeaderView.frame;
        frame.size.height = height;
        self.tableHeaderView.frame = frame;
        self.tableView.tableHeaderView = self.tableHeaderView;
    }];

    [searchBar setShowsCancelButton:YES animated:YES];
    return YES;
}

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
    searchBar.showsScopeBar = NO;
    [UIView animateWithDuration:0.f animations:^{
        [searchBar sizeToFit];

        CGFloat height = CGRectGetHeight(searchBar.frame);

        CGRect frame = self.tableView.tableHeaderView.frame;
        frame.size.height = height;
        self.tableHeaderView.frame = frame;
        self.tableView.tableHeaderView = self.tableHeaderView;
    }];

    [searchBar setShowsCancelButton:NO animated:YES];
    return YES;
}

분명히, 지금쯤이면 Apple은 UITableView를 구현했어야 합니다.테이블의 자동 치수머리글 보기 & 테이블 바닥글 보기...

레이아웃 제약 조건을 사용하면 다음과 같은 작업이 가능한 것 같습니다.

CGSize   s  = [ self  systemLayoutSizeFittingSize : UILayoutFittingCompressedSize ];
CGRect   f  = [ self  frame ];

f.size   = s;

[ self  setFrame : f ];

테이블에 있는 경우HeaderView는 내용을 조정할 수 있는 WebView이며, 다음을 시도할 수 있습니다.

[self.webView.scrollView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil];

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    self.webView.height = self.webView.scrollView.contentSize.height;
    self.tableView.tableHeaderView = self.webView;
}

iOS9과 iOS11에서 테스트해봤는데 잘 작동했습니다.

해보셨습니까[self.tableView reloadData]높이를 바꾼 후에?

언급URL : https://stackoverflow.com/questions/341256/how-to-resize-a-tableheaderview-of-a-uitableview

반응형