Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SearchBar 边框圆角设置不了 #151

Open
AshineRong opened this issue Dec 27, 2017 · 13 comments
Open

SearchBar 边框圆角设置不了 #151

AshineRong opened this issue Dec 27, 2017 · 13 comments

Comments

@AshineRong
Copy link

image
边框的圆角,大小设置无效?

@AshineRong
Copy link
Author

@ko1o 自定义搜索框的话,正确设置方式是?

@ko1o
Copy link
Owner

ko1o commented Jan 3, 2018

@JiaHaoGong

  1. 继承PYSearchViewController
  2. 重写- (void)viewDidLayoutSubviews方法

@fengios
Copy link

fengios commented Jun 14, 2018

@ko1o
继承PYSearchViewController
重写- (void)viewDidLayoutSubviews方法
还是不能设置搜索框圆角

@AllLuckly
Copy link

只能改大,不能改小、、、、、

@baocang
Copy link
Contributor

baocang commented Oct 31, 2018

@AllLuckly _UISearchBarSearchFieldBackgroundView 就是 UIImageView,不能改小的原因是里面有图片, 解决这个问题的方法就是
使用一张没有圆角的图片
通过代码生成对应的图片
直接不用图片,通过backgroundColor和cornerRadius来控制。
临时解决方案可以通过 继承后 重写 setSearchBarCornerRadius

@baocang
Copy link
Contributor

baocang commented Oct 31, 2018

@ko1o 是否应该考虑用北背景色代替背景图片来解决这个问题?这种方案倒是最简单的。

@AllLuckly
Copy link

@baocang

<_UISearchBarSearchFieldBackgroundView: 0x7f859372ba70; frame = (0 0; 0 0); opaque = NO; autoresize = W+H; userInteractionEnabled = NO; layer = <CALayer: 0x6000032820a0>>

这个size一直是0 重新好像也没用

@baocang
Copy link
Contributor

baocang commented Oct 31, 2018

@AllLuckly
不要用frame 调整,直接设置图片和背景色。如果要调整frame,你得记得它自己内部还有布局,比较烦。
比如设置圆角为0:

searchViewController.searchBarCornerRadius = 0;

只需要做下面的变更:

    - (void)setSearchBarCornerRadius:(CGFloat)searchBarCornerRadius
         if ([NSStringFromClass([subView class]) isEqualToString:@"_UISearchBarSearchFieldBackgroundView"]) {
             subView.layer.cornerRadius = searchBarCornerRadius;
             subView.clipsToBounds = YES;
+            if ([subView isKindOfClass:UIImageView.class]) {
+                [(UIImageView *)subView setImage:nil];
+            }
+            subView.backgroundColor = [UIColor colorWithRed:142.0f/255.0f green:142.0f/255.0f blue:147.0f/255.0f alpha:0.12f];
             break;
         }
     }

你自己暂时继承一下 PYSearchViewController,功过重写 setSearchBarCornerRadius 来实现
其余的我稍后还是提交个PR吧,内容是允许自定义 searchBar的背景色和设置更小的圆角。这个需求应该还是需要满足的。

@baocang
Copy link
Contributor

baocang commented Oct 31, 2018

@AllLuckly
刚看了下源码,你还可以这样做,(至于刚刚说的PR,我跟作者交流后看用哪种方案再说,上面给出的方案看起来不怎么好):

searchViewController.searchTextField.borderStyle = UITextBorderStyleNone;
searchViewController.searchTextField.layer.cornerRadius = 2;
searchViewController.searchTextField.clipsToBounds  = YES;
searchViewController.searchTextField.backgroundColor = [UIColor py_colorWithHexString:@"8E8E93" alpha:0.12f];

@baocang
Copy link
Contributor

baocang commented Oct 31, 2018

@ko1o 这个问题并不严重,而且不需要改 PYSearch 源码也能实现。但是这里已经公开了 searchBarCornerRadiussearchBarBackgroundColor 属性,所以需要讨论下是否需要把原本默认的样式也改掉。然后全部通过自定义属性 searchBarCornerRadiussearchBarBackgroundColor 的默认值来控制 searchBar的样式。

@CivelXu
Copy link

CivelXu commented May 5, 2019

        if let searchTextField = pySearch.searchBar.value(forKey: "searchField") as? UITextField {
            
            searchTextField.borderStyle = .none
            searchTextField.layer.cornerRadius = 4
            searchTextField.layer.masksToBounds = true
            searchTextField.backgroundColor = AppColors.F2F2F2
            searchTextField.font = UIFont.systemFont(ofSize: 15)
            
            if #available(iOS 11, *) {
                searchTextField.frame = CGRect(x: 12, y: 8, width: kScreenW() * 0.8, height: 28)
            } else {
                searchTextField.frame = CGRect(x: 0, y: 8, width: kScreenW() * 0.8, height: 28)
            }
            
            let leftView = UIImageView(image: UIImage.init(named: "search"))
            leftView.size = CGSize(width: 15, height: 15)
            searchTextField.leftView = leftView
            pySearch.searchBar.setValue(searchTextField, forKey: "searchField")
        }

设置圆角最正确的方式

@Shawkaine
Copy link

iOS13 不管用了

        if let searchTextField = pySearch.searchBar.value(forKey: "searchField") as? UITextField {
            
            searchTextField.borderStyle = .none
            searchTextField.layer.cornerRadius = 4
            searchTextField.layer.masksToBounds = true
            searchTextField.backgroundColor = AppColors.F2F2F2
            searchTextField.font = UIFont.systemFont(ofSize: 15)
            
            if #available(iOS 11, *) {
                searchTextField.frame = CGRect(x: 12, y: 8, width: kScreenW() * 0.8, height: 28)
            } else {
                searchTextField.frame = CGRect(x: 0, y: 8, width: kScreenW() * 0.8, height: 28)
            }
            
            let leftView = UIImageView(image: UIImage.init(named: "search"))
            leftView.size = CGSize(width: 15, height: 15)
            searchTextField.leftView = leftView
            pySearch.searchBar.setValue(searchTextField, forKey: "searchField")
        }

设置圆角最正确的方式

@Shawkaine
Copy link

@AllLuckly
刚看了下源码,你还可以这样做,(至于刚刚说的PR,我跟作者交流后看用哪种方案再说,上面给出的方案看起来不怎么好):

searchViewController.searchTextField.borderStyle = UITextBorderStyleNone;
searchViewController.searchTextField.layer.cornerRadius = 2;
searchViewController.searchTextField.clipsToBounds  = YES;
searchViewController.searchTextField.backgroundColor = [UIColor py_colorWithHexString:@"8E8E93" alpha:0.12f];

你这就是iOS 13的官方修改方法……ww

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants