ios - 当 superview 布置 subview 时,UISearchBar 动画到位。如何禁用?

标签 ios swift uiview uikit uiviewanimation

我的问题: 我有一个使用 UINavigationController 的用户入职流程介绍入职流程中的每个步骤。使用 UISearchBar 推送 View Controller 时在其中,UISearchBar动画到位。

我的问题是什么: https://youtu.be/TKPLeJ9FmI4 (问题发生在 0:270:320:41 附近)

我收集到的信息: 这似乎发生在堆栈中的前一个 View Controller 在推送下一个 View Controller 之前执行异步函数时,并且可能没有完成异步函数直到 UISearchBar 之后已经动画到位。

我尝试过的: 我尝试子类化 UISearchBar并确保layoutSubviews不是通过 UIView.performWithoutAnimation 制作动画的:

import UIKit

class SearchBar: UISearchBar {
    override func layoutSubviews() {
        UIView.performWithoutAnimation {
            super.layoutSubviews()
        }
    }
}

同样,我尝试确保 UISearchBar布置 subview 时,父 View 没有动画:

class MyViewController: UIViewController {
    @IBOutlet var searchBar: UISearchBar!

    override func viewDidLayoutSubviews() {
        UIView.performWithoutAnimation {
            super.viewDidLayoutSubviews()
        }
    }
}

我想知道的是:由于很难确定动画的确切原因,我很好奇是否有一种方法可以简单地防止 UISearchBar从完全动画。我永远不会真正需要为 UISearchBar 设置动画。 .

最佳答案

这在 Objective-C 中对我有用。看起来你有足够的能力将它翻译成 Swift:

  [[UISearchBar appearance] setBackgroundImage:[UIImage new]
                                forBarPosition:UIBarPositionAny
                                    barMetrics:UIBarMetricsDefault];

  [[UISearchBar appearance] setBackgroundImage:[UIImage new]
                                forBarPosition:UIBarPositionAny
                                    barMetrics:UIBarMetricsDefaultPrompt];

[self.searchBar setSearchFieldBackgroundImage:finalImage forState:UIControlStateNormal];

其中“finalImage”是您在代码中绘制的图像,它返回与 UISearchBar 相同矩形大小的 UIImage。您可以找到方法来绘制您想要的背景颜色的图像,然后将图像的角圆化。在 Objective-C 中也是如此。

- (UIImage *)imageWithColor:(UIColor *)color withSize:(CGRect)imageRect {
  UIGraphicsBeginImageContext(imageRect.size);
  CGContextRef context = UIGraphicsGetCurrentContext();

  CGContextSetFillColorWithColor(context, [color CGColor]);
  CGContextFillRect(context, imageRect);

  UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();

  return image;
}

- (UIImage *)roundImage:(UIImage *)image withRadius:(CGFloat)radius {
  CGRect rect = CGRectMake(0.0, 0.0, 0.0, 0.0);
  rect.size = image.size;
  UIGraphicsBeginImageContextWithOptions(image.size, NO, [UIScreen mainScreen].scale);
  UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect
                                                  cornerRadius:radius];
  [path addClip];
  [image drawInRect:rect];

  image = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();

  return image;
}

所以 UISearchBar 的完整声明是这样的:

  self.searchBar = [UISearchBar new];
  CGRect rect = CGRectMake(0.0, 0.0, 44, 35);
  UIImage *colorImage = [self imageWithColor:[UIColor whiteColor] withSize:rect];
  UIImage *finalImage = [self roundImage:colorImage withRadius:10.0];
  [self.searchBar setSearchFieldBackgroundImage:finalImage forState:UIControlStateNormal];
  [self.searchBar setTranslatesAutoresizingMaskIntoConstraints:false];
  [self.view addSubview:self.searchBar];

这是我发现的唯一一种删除 Apple 添加的所有时髦动画并根据需要自定义背景的方法。祝你好运(对于那些因为它不在 Swift 中而对此投反对票的人,你应该知道 Swift 是 Ruby 2.0 和一种模因语言,直到真正的编码人员让它不是一种模因语言,所以坚持使用 Objective-C)。

祝你好运

编辑

如果使用布局约束,请确保设置约束,如果不使用约束,则移除

  [self.searchBar setTranslatesAutoresizingMaskIntoConstraints:false];

然后设置UISearchBar的frame

关于ios - 当 superview 布置 subview 时,UISearchBar 动画到位。如何禁用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58563300/

相关文章:

ios - 如何实现 iOS 9 的 UICollectionView 交互移动功能?

swift - 计分系统多次计分

c++ - 在闭包中,如何通过指针或引用类型捕获的可变变量存储在内存中或以现代功能语言处理?

ios - 它不会构建失败,它会显示 "No Issues"但仍然无法正常工作

ios - 下载图像数据时显示进度的最简单方法是什么? [ swift ]

iOS : perform task in background when app is force quit by user

ios - 在iOS7上未调用didRegisterForRemoteNotificationsWithDeviceToken

iphone - iOS 中每个 View Controller 的静态 View

ios - 来自 CGRectMake 的 UIView 没有正确的高度

ios - 由于弃用而替换 self->isa