I'm trying to remove the textborderstyle of the UITextField in a UISearchBar. I know that I'm getting reference to the correct object, as I'm able to change the TextBorderStyle to RoundedRect and see a visible change.
I've tried subclassing UISearchBar with the following code:
- (void)layoutSubviews {
UITextField *searchField;
NSUInteger numViews = [self.subviews count];
for(int i = 0; i < numViews; i++) {
if([[self.subviews objectAtIndex:i] isKindOfClass:[UITextField class]]) { //conform?
searchField = [self.subviews objectAtIndex:i];
}
}
if(!(searchField == nil)) {
[searchField setBorderStyle:UITextBorderStyleNone];
}
[super layoutSubviews];
}
I've also tried this inside a class:
UITextField *txtSearchField = [_searchBar valueForKey:#"_searchField"];
[txtSearchField setBackgroundColor:[UIColor clearColor]];
[txtSearchField setBorderStyle:UITextBorderStyleNone];
Neither of these work. By default, it seems to have UITextBorderStyleLine if I manually set it to None. I unfortunately haven't been able to find a way to remove the border style. Does anyone know how to do this? I'm trying to figure this out since I need my search bar to look like a UITextField. I'm debating just switching to a UITextField behind the scenes at this point.
I'm pretty certain that the only way to achieve this is to subclass UISearchBar!
Try this code in your layoutSubviews method once you have subclassed it!
- (void)layoutSubviews {
UITextField *searchField;
NSUInteger numViews = [self.subviews count];
for(int i = 0; i < numViews; i++) {
if([[self.subviews objectAtIndex:i] isKindOfClass:[UITextField class]]) { //conform?
searchField = [self.subviews objectAtIndex:i];
}
}
if(!(searchField == nil)) {
searchField.textColor = [UIColor whiteColor];
[searchField setBackground: [UIImage imageNamed:#"buscador.png"] ];
[searchField setBorderStyle:UITextBorderStyleNone];
}
[super layoutSubviews];
}
The question found here remove the border of uitextfield in uisearchbar answers your initial question!
You can also reference How to customize apperance of UISearchBar for more information!
Hope this helps!
Just not to overlook the comment (I just did, when I landed in this same issue and came to this question). If,programmatically, your UITextField's borderStyle does 'not' get set to UITextBorderStyleNone, then try setting the background or backgroundColor property of the UITextField 'before' setting its borderStyle! And it works!
Related
I am using the following to try to access all the UIButtons located inside of a UIScrollView within a UIView. The problem is that the code doesn't seem to locate the buttons and set the border property.
UIView -> UIScrollView -> UIButtons.
I basically want to loop through the buttons and set the border property.
for(UIView *v in [self.viewLightLeakChoices subviews]) {
if([v isKindOfClass:[UIButton class]]) {
v.layer.borderWidth = 0;
}
}
Try this one instead
for (id obj in scrollView.subviews) {
NSString *classStr = NSStringFromClass([obj class]);
if ([classStr isEqualToString:#"UIButton"]) {
UIButton *button = (UIButton*)obj;
button.layer.borderWidth = 2.0;
button.layer.borderColor = [UIColor greenColor].CGColor;
}
}
output
Thanks for the help guys. I ended up doing a bunch of for loops to get down to the UIButtons.
for(UIView *v in [self.viewLightLeakChoices subviews]) {
if([v isKindOfClass:[UIScrollView class]]) {
for(UIView *subView in [v subviews]) {
for(UIButton *btn in [subView subviews]) {
btn.layer.borderWidth = 0;
}
}
}
}
first make sure you are getting subviews of UIScrollView because your structure is
UIView > UIScrollView> UIButton
if you have only 1 scroll view in self.viewLightLeakChoices then set your scrollview tag = 1000 and direct access your scrollview so now you dont need to use loop. and execution will be fast.
UIScrollView *scrlV = [self.viewLightLeakChoices viewWithTag:1000];
for (UIButton *btn in scrlV.subviews)
{
if ([btn isKindOfClass:[UIButton Class]]) {
btn.layer.borderWidth = 1.0;
btn.layer.borderColor = [UIColor whiteColor].CGColor;
}
}
i am unable to get i am expecting you are having scrollview -> uiview ->button
for(UIView *myview in Scrollview.subviews)
{
for ( id mybutton in myview.subviews)
{
if ([mybutton isKindOfClass:[UIButton class]])
{
UIButton *mybtn=(UIButton *)mybutton;
mybtn.layer.borderWidth=0;
}
}
}
You should have create Button class say MyButton extents UIButton, if you do this there is no need to loop through scrollview's subviews.
implement awakeFromNib method and apply border, to apply border refer this SO Post How to create border in UIButton?
Can I adjust the height of the UITableview's separator line? I add UIView at the cell to use as separator line and its good, the problem is that when I slide the cell to delete it, the delete button is the problem, its overlapping the separator line, or can I adjust the delete button's height?
The code pasted in by Rashad is pretty old (found here) and doesn't seem to work for iOS 7 or iOS 8.
Here is updated code that works:
-(void)layoutSubviews {
UIView *deleteButtonView = nil;
for (UIView *subview in self.subviews) {
// find the delete view in iOS 8
if ([NSStringFromClass([subview class]) isEqualToString:#"UITableViewCellDeleteConfirmationView"]){
deleteButtonView = subview;
break;
}
// find the delete view in iOS 7
if ([NSStringFromClass([subview class]) isEqualToString:#"UITableViewCellScrollView"]) {
for (UIView *secondSubview in [subview subviews]) {
if ([NSStringFromClass([secondSubview class]) isEqualToString:#"UITableViewCellDeleteConfirmationView"]) {
deleteButtonView = secondSubview;
break;
}
}
}
}
int heightOffset = 5;
CGRect buttonFrame = deleteButtonView.frame;
buttonFrame.origin.y = heightOffset;
buttonFrame.size.height = self.frame.size.height-2*heightOffset;
deleteButtonView.frame = buttonFrame;
}
If you can't resize the delete button, resize your bottom UIView so it can overlap the delete button.
I always draw separator line like a subView on contentView of cell. And disable separatorStyle in tableView. And customise delete button like here: https://stackoverflow.com/a/22396248/887325
In you TableViewCell layoutSubviews method write this:
if ([NSStringFromClass([subview class]) isEqualToString:#"UITableViewCellDeleteConfirmationControl"]) {
UIView *deleteButtonView = (UIView *)[subview.subviews objectAtIndex:0];
CGRect newf = deleteButtonView.frame;
newf.origin.x = 250;
newf.origin.y = 47;
newf.size.width = 30;
newf.size.height = 50;
deleteButtonView.frame = newf;
}
Hope this helps.. :)
I tried, in IOS 7 to subclass an UISearchBar so that the place holder is always left aligned.
I did this:
- (void)layoutSubviews {
[super layoutSubviews];
UITextField * tv = self.textField;
tv.textAlignment = NSTextAlignmentLeft ;
}
If tv.textAlignment = NSTextAlignmentRight, then I manage to make the text to the right.
However, the text when UISearchBar is empty, and display the placeholder, always shows to the center.
I wonder why. I put it in layoutSubviews method. Hence, it should be drawn every time the control is drawn.
Can you try this using UIAppearance.
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextAlignment:NSTextAlignmentLeft];
Downvoters, please read this : note from Apple:
Note: iOS applies appearance changes when a view enters a window, it doesn’t change the appearance of a view that’s already in a window. To change the appearance of a view that’s currently in a window, remove the view from the view hierarchy and then put it back.
I have same issue before some days ago, but there is no way to change the alignment of search bar's placeholder text, I tested with so many "SO" answer but no one was working.
At last I decided with following Fixing.
Add some white space in Left/right (as you want) of placeholder text
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 6.1) {
// Load resources for iOS 6.1 or earlier
self.searchBar.placeholder = #"hello";
}
else
{
// Load resources for iOS 7 or later
// Add some white space in Left/right *(as you want)* here i added to left
self.searchBar.placeholder = #" hello";
}
Through NSLog the searchBar's textField subviews, can get such a result:
<_UISearchBarSearchFieldBackgroundView: 0x8d492e0; frame = (0 0; 304 28); opaque = NO; autoresize = W+H; userInteractionEnabled = NO; layer = <CALayer: 0x8d49410>> - (null),
<UIImageView: 0x8d488a0; frame = (102.5 7.5; 12.5 12.5); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x8d48f80>> - (null),
<UISearchBarTextFieldLabel: 0x8d4b410; frame = (122.5 1; 181.5 25); text = 'hello000000'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x8d4b520>>
)
you can see, UISearchBarTextFieldLabel is the UI to show placeholder, here is 'hello000000', which is a subclass of UILabel. Therefore, set searchBar's textField textAlignment will not affect placeholder position directly. And the layout of placeholder label is handled by searchBar's textField - (void)layoutSubviews , whereas you can't override inner textField this method.
A custom searchBar base on UIView, add your subclass of UITextField which override - (void)layoutSubviews, Or even add add UILabel where textfield's text is empty, remove while not. maybe a solution.
Add some my test code:
#interface CustomSearchBar : UIView {
}
#end
#implementation SharedSearchBar
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
_textField = [[SearchTextField alloc] initWithFrame:CGRectMake(10, 6, frame.size.width-20, frame.size.height-12)];
_textField.leftView = UIImageViewNamed(#"search_glass");
[self addSubview:_textField];
}
return self;
}
#end
#interface SearchTextField : UITextField
#end
#implementation SearchTextField
- (void)layoutSubviews{
[super layoutSubviews];
for (UIView *subView in self.subviews) {
if ([subView isKindOfClass:[UILabel class]]) {
UILabel *lb = (UILabel *)subView;
lb.textAlignment = UITextAlignmentRight;
lb.text = #"123123";
CGRect frame = lb.frame;
frame.origin.x = textFiled.frame.size.width-frame.size.width;
lb.frame = frame;
break;
}
}
}
#end
I am working on IOS 7 application.By default its appearing like Pic(1).But I need to change it as Pic(2).I googled and found few answers for the requirement,but it has not changed.Or else I need to hide.So that I can manage with background image.This is first image
I used below code to modify it.But didnt succeed.
In .h file
#property(nonatomic,strong) IBOutlet UISearchBar *findSearchBar;
In .m file
#synthesize findSearchBar;
- (void)viewDidLoad
{
[super viewDidLoad];
[self setSearchIconToFavicon];
}
- (void)setSearchIconToFavicon
{
// The text within a UISearchView is a UITextField that is a subview of that UISearchView.
UITextField *searchField;
for (UIView *subview in self.findSearchBar.subviews)
{
if ([subview isKindOfClass:[UITextField class]]) {
searchField = (UITextField *)subview;
break;
}
}
if (searchField)
{
UIView *searchIcon = searchField.leftView;
if ([searchIcon isKindOfClass:[UIImageView class]])
{
NSLog(#"aye");
}
searchField.rightView = nil;
searchField.leftView = nil;
searchField.leftViewMode = UITextFieldViewModeNever;
searchField.rightViewMode = UITextFieldViewModeAlways;
}
}
I am not getting how to make the center of the view's image to nil.Its really killing my time.Please help me.where I had gone wrong.
UITextField *txfSearchField = [looksearchbar valueForKey:#"_searchField"];
[txfSearchField setBackgroundColor:[UIColor whiteColor]];
[txfSearchField setLeftViewMode:UITextFieldViewModeNever];
[txfSearchField setRightViewMode:UITextFieldViewModeNever];
[txfSearchField setBackground:[UIImage imageNamed:#"searchbar_bgImg.png"]];
[txfSearchField setBorderStyle:UITextBorderStyleNone];
//txfSearchField.layer.borderWidth = 8.0f;
//txfSearchField.layer.cornerRadius = 10.0f;
txfSearchField.layer.borderColor = [UIColor clearColor].CGColor;
txfSearchField.clearButtonMode=UITextFieldViewModeNever;
Try this may be it will help u........
I'm not sure how to left-align the placeholder, but as of iOS 5.0 there's a simple, supported way to modify the search bar's text field properties, e.g.:
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setLeftViewMode:UITextFieldViewModeNever];
which will hide the magnifying glass icon.
You could try:
searchBar.setImage(UIImage(named: "yourimage")!, forSearchBarIcon: UISearchBarIcon.Clear, state: UIControlState.Normal)
I created a search bar programmatically and added to my view using the codes below:
- (void)viewDidLoad
{
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(xPositionForSearchBar, yPositionForSearchBar, widthForSearchBar, heightForSearchBar)];
UIView *bg = [[searchBar subviews] objectAtIndex:0];
searchBar.delegate = self;
searchBar.placeholder = #"Search record";
for(UIView *view in searchBar.subviews){
if([view isKindOfClass:[UITextField class]]){
UITextField *tf = (UITextField *)view;
tf.delegate = self;
break;
}
}
[bg removeFromSuperview];
[self.view addSubview: searchBar];
}
The code is implemented with UISearchBarDelegate and UITextFieldDelegate.
I have tried using
- (void)searchBarCancelButtonClicked:(UISearchBar *)aSearchBar
{
NSLog(#"cancel clicked");
searchBar.text = #"";
[aSearchBar resignFirstResponder];
}
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
NSLog(#"clear");
[self performSelector:#selector(searchBarCancelButtonClicked:) withObject:searchBar afterDelay: 0.1];
return YES;
}
and yet, the text inside the searchBar is not cleared at all when i click on the "clear button" - a circle with a "X" inside.
The clear button works when I implemented it in IB. Wonder why?
Kindly advice, many thanks.
This might happen if you position your search bar out of the bounds of a parent view. Then, the touches aren't delivered to your searchBar properly. This also affects text editing controls like copy & paste.
I have checked your code it work fine on device as well as on simulator.