pr1*_*001 22 spacing uisearchbar ios
当我在a上设置搜索字段背景图像UIImage
时,选中更改时,搜索栏中放大镜和占位符文本之间的填充.
使用默认背景:
使用自定义背景:
这种变化是由以下两行引起的:
UIImage *colorImage = [UIImage imageWithColor:[UIColor grayColor] size:CGSizeMake(28, 28)];
[self setSearchFieldBackgroundImage:[colorImage imageWithRoundedCorners:5] forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)
imageWithRoundedCorners:
是一种类别方法,它只是将图像绘制到CALayer
具有角半径的图像上,然后UIImage
从图形上下文创建一个.
为什么这样,我怎么能避免这个?我尝试传递一个显式可调整大小的图像,但这没有任何效果.
Rya*_*los 31
奇怪的是它会重置它,但是你可以使用这样的东西在设置背景图像后根据自己的喜好设置它.searchTextPositionAdjustment
是UISearchBar上的一个属性,完美无缺.8.0似乎是默认值,但您可以将其设置为您喜欢的任何内容.
[self.searchBar setSearchFieldBackgroundImage:[self imageWithColor:[UIColor grayColor] andSize:CGSizeMake(28.0, 28.0)] forState:UIControlStateNormal];
[self.searchBar setSearchTextPositionAdjustment:UIOffsetMake(8.0, 0.0)];
Run Code Online (Sandbox Code Playgroud)
Ben*_*Ben 12
对于现在使用Swift的所有人,您可以使用以下代码(适用于Swift 2)
let searchBarBackground = UIImage.roundedImage(UIImage.imageWithColor(UIColor.whiteColor(), size: CGSize(width: 28, height: 28)),cornerRadius: 2)
searchBar.setSearchFieldBackgroundImage(searchBarBackground, forState: .Normal)
searchBar.searchTextPositionAdjustment = UIOffsetMake(8.0, 0.0)
Run Code Online (Sandbox Code Playgroud)
使用此扩展名为UIImage:
extension UIImage {
class func imageWithColor(color: UIColor, size: CGSize) -> UIImage {
let rect: CGRect = CGRectMake(0, 0, size.width, size.height)
UIGraphicsBeginImageContextWithOptions(size, false, 0)
color.setFill()
UIRectFill(rect)
let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
class func roundedImage(image: UIImage, cornerRadius: Int) -> UIImage {
let rect = CGRect(origin:CGPoint(x: 0, y: 0), size: image.size)
UIGraphicsBeginImageContextWithOptions(image.size, false, 1)
UIBezierPath(
roundedRect: rect,
cornerRadius: CGFloat(cornerRadius)
).addClip()
image.drawInRect(rect)
return UIGraphicsGetImageFromCurrentImageContext()
}
}
Run Code Online (Sandbox Code Playgroud)
给出这样的东西:
归档时间: |
|
查看次数: |
5315 次 |
最近记录: |