我的问题:
我有一个使用 UINavigationController
的用户入职流程介绍入职流程中的每个步骤。使用 UISearchBar
推送 View Controller 时在其中,UISearchBar
动画到位。
我的问题是什么: https://youtu.be/TKPLeJ9FmI4 (问题发生在 0:27 、 0:32 、 0: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/