日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[iOS] tableView中实现底部button出现时tableView的bottom自动向上偏移

發(fā)布時間:2023/12/18 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [iOS] tableView中实现底部button出现时tableView的bottom自动向上偏移 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

這是我在工程中遇到的一個需求:選擇照片之后,按bottomButton進(jìn)行發(fā)送。

具體場景:照片存放在tableviewcontroller中,當(dāng)選擇照片之后,自動彈出bottomButton,點擊之后發(fā)送照片;當(dāng)取消所有的照片之后該button自動消失。

問題:該button會遮擋底部的照片,所以需要實現(xiàn)當(dāng)?shù)撞康腷utton出現(xiàn)時tableView的bottom自動向上偏移的功能。

? 我用autoLayout和contentOffset來解決這個問題的,在一開始就創(chuàng)建這兩個view(tableview 和bottomButton),通過控制NSLayoutConstant的constant來控制該button的出現(xiàn),省去了hidden = YES/NO;下面通過代碼進(jìn)行分析:

- (void)setupUI

{

? [self createPhotoView];

? [self sendButtonView];

? // auto layout

? self.assetsViewController.view.translatesAutoresizingMaskIntoConstraints = NO;

? self.sendButtonView.translatesAutoresizingMaskIntoConstraints = NO;

? NSDictionary *viewsDict = @{@"assetsView": self.assetsViewController.view,

?? ? ? ? ? ? ? ? ? ? ? ? ? ? @"sendBtnView": self.sendButtonView};

? [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[assetsView]-0-|" options:0 metrics:nil views:viewsDict]];

? [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[sendBtnView]-0-|" options:0 metrics:nil views:viewsDict]];

? [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[assetsView]-0-[sendBtnView]-0-|" options:0 metrics:nil views:viewsDict]];

? self.sendBtnHeightLayoutConstraint = [NSLayoutConstraint constraintWithItem:self.sendButtonView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0f constant:0];

? [self.view addConstraint:self.sendBtnHeightLayoutConstraint];

}

這部分控制了該button的水平和垂直方向,主要是垂直方向的控制。關(guān)鍵是?self.sendBtnHeightLayoutConstraint變量比較巧妙,這是我之前申請的一個屬性:

@property (nonatomic, strong) NSLayoutConstraint *sendBtnHeightLayoutConstraint;

而?self.sendBtnHeightLayoutConstraint = [NSLayoutConstraint constraintWithItem:self.sendButtonView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0f constant:0];實際通過對該變量進(jìn)行賦值以控制其隱藏或出現(xiàn),下面會繼續(xù)分析。

?

接著是顯示button的函數(shù):

- (void)updateBottomButton:(NSInteger)selectedItemCount

{

?// use animation?

? [UIView animateWithDuration:0.5f animations:^{

? ? if (0 >= selectedItemCount) {//hide

? ? ? self.sendBtnHeightLayoutConstraint.constant = 0;

? ? ? self.tableViewAdjustOffsetY = 0.0;

? ? } else { //show

? ? ? self.sendBtnHeightLayoutConstraint.constant = kHeightForSendButton;

? ? ?// three conditions

? ? ? BOOL isMoreThanOnePage = self.assetsViewController.tableView.contentSize.height > self.assetsViewController.tableView.frameHeight;

? ? ? BOOL isInLastPage = self.assetsViewController.tableView.contentOffset.y > self.assetsViewController.tableView.contentSize.height - self.assetsViewController.tableView.frameHeight;

? ? ? BOOL hasAdjustedOffsetY = self.assetsViewController.tableView.contentOffset.y == self.tableViewAdjustOffsetY;

? ? ? if (isMoreThanOnePage && isInLastPage && !hasAdjustedOffsetY) {

? ? ? ? self.tableViewAdjustOffsetY = self.assetsViewController.tableView.contentOffset.y + kHeightForSendButton;

? ? ? ? self.assetsViewController.tableView.contentOffset = CGPointMake(self.assetsViewController.tableView.contentOffset.x, self.tableViewAdjustOffsetY);

? ? ? }

? ? }

? ? [self.view layoutIfNeeded];

? } completion:^(BOOL finished) {

? }];

其中self.sendBtnHeightLayoutConstraint.constant = 0;將該button進(jìn)行隱藏,這個用法比較巧妙,因為前面已經(jīng)?[self.view addConstraint:self.sendBtnHeightLayoutConstraint];所以這里的改變直接影響到self.view,但前提是要調(diào)用[self.view layoutIfNeeded];

在show的分支中,?self.sendBtnHeightLayoutConstraint.constant = kHeightForSendButton;進(jìn)行顯示。

?

接著是contentOffset的控制,之前的3個condition很容易考慮不全。isInLastPage:因為我們需要在最后一頁移動tableview的bottom;isMoreThanOnePage:并且如果只有一頁的話我們不需要進(jìn)行操作;!hasAdjustedOffsetY:在連續(xù)選擇時只在第一次時移動。

最后有個問題,為啥show的適合需要調(diào)整contentOffset,hide的時候不需要呢?似乎也很簡單,聰明的你應(yīng)該已經(jīng)知道答案了吧!

轉(zhuǎn)載于:https://www.cnblogs.com/sweet-coffee/p/4934156.html

總結(jié)

以上是生活随笔為你收集整理的[iOS] tableView中实现底部button出现时tableView的bottom自动向上偏移的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。