I have used this piece of code to set Text Color for the Texfield Inside the UISearchBar.
for (UIView* view in subViews)
{
if([view isKindOfClass:[UITextField class]])
{
UITextField* searchTextField = (UITextField*)view;
[self setTextColorToTextField:searchTextField];
break;
}
else
{
for (id view1 in view.subviews)
{
if ([view1 isKindOfClass:[UITextField class]])
{
UITextField* searchTextField = (UITextField*)view1;
[self setTextColorToTextField:searchTextField];
break;
}
}
}
}
Can anyone suggest how to improve the code?
Well there is no need to code when you can set its colour in the StoryBoard.
Here add this line in identity inspector of searchBar.
Related
I want to change cursor position in textview of SLComposeViewController to beginning of UITextView
currently is show me the end of initial Text:
Currently Cursor
but I want cursor at first position in UITextView. with (iOS 9 support)
I also use this code
[self presentViewController:sharingComposer animated:NO completion:^{
for (UIView *viewLayer1 in sharingComposer.view.subviews) {
for (UIView *viewLayer2 in viewLayer1.subviews) {
if ([viewLayer2 isKindOfClass:[UIView class]]) {
for (UIView *viewLayer3 in viewLayer2.subviews) {
if ([viewLayer3 isKindOfClass:[UITextView class]]) {
[(UITextView *)viewLayer3 setDelegate:self];
sharingTextView = (UITextView *)viewLayer3;
}
}
}
}
}
}];
but i can't get sharing UITextView and not working for me.
Try this:-
UITextPosition *beginning = [sharingTextView beginningOfDocument];
[sharingTextView setSelectedTextRange:[textField textRangeFromPosition:beginning
toPosition:beginning]];
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?
I am customizing my search bar with this but it is not showing any image. It remove its default image as well.
UITextField *searchField = nil;
for (UIView *subview in [[workLocationSearchBar.subviews lastObject] subviews]) {
NSLog(#"hey");
if ([subview isKindOfClass:[UITextField class]]) {
searchField = (UITextField *)subview;
NSLog(#"inside");
break;
}
}
if (searchField) {
UIView *searchIcon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"circle.png"]];
if ([searchIcon isKindOfClass:[UIImageView class]]) {
}
NSLog(#"aye");
searchField.rightView = searchIcon;
searchField.leftView=searchIcon;
}
Try This:
[workLocationSearchBar setImage:[UIImage imageNamed:#"IMAGE_NAME"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
I am developing an application. In that I get all subviews of UITableViewCell.
The code for this one is:
(void)listSubviewsOfView:(UIView *)view {
// Get the subviews of the view
NSArray *subviews = [view subviews];
// Return if there are no subviews
if ([subviews count] == 0) return;
for (UIView *subview in subviews) {
NSLog(#"%#", subview);
// List the subviews of subview
[self listSubviewsOfView:subview];
}
}
But my problem is how to find out button from that subviews list. Please tell me how to solve this one.
You can iterate through all subviews like this.
for (id subview in subviews) {
if ([subview isKindOfClass:[UIButton class]]) {
//do your code
}
}
Swift
for subview in view.subviews {
if subview is UIButton {
// this is a button
}
}
Check if the subview is a button with is AnyClass, isKind(of aClass: AnyClass) or isMember(of aClass: AnyClass) API.
Using is-
for subview in self.view.subviews{
if subview is UIButton{
//do whatever you want
}
}
Using isMember(of:)-
for subview in self.view.subviews{
if subview.isMember(of: UIButton.self){
//do whatever you want
}
}
Using isKind(of:)-
for subview in self.view.subviews{
if subview.isKind(of: UIButton.self){
//do whatever you want
}
}
To expand on the 'do your code' part of Martin's answer. Once I got the subview, I wanted to test whether it was the right button and then remove it. I can't check the titleLabel of the button directly by using subview.titleLabel so I assigned the subview to a UIButton and then checked the button's titleLabel.
- (void)removeCheckboxes {
for ( id subview in self.parentView.subviews ) {
if ( [subview isKindOfClass:[UIButton class]] ) {
UIButton *checkboxButton = subview;
if ( [checkboxButton.titleLabel.text isEqualToString:#"checkBoxR1C1"] ) [subview removeFromSuperview];
}
}
}
Which is the correct way of enumerating through sub views to find text fields?
NSMutableArray *mutableTFs = [[NSMutableArray alloc] init];
for (UIView *view in [self.view subviews]) {
if ([view isKindOfClass:[UITextField class]]) {
[mutableTFs addObject:view];
}
}
OR
NSMutableArray *mutableTFs = [[NSMutableArray alloc] init];
for (UITextField *textField in [self.view subviews]) {
[mutableTFs addObject:textField];
}
I know this isn't the correct wording, but what I don't understand is if it is the top method, how do you 'convert' it from a view to a text field?
Which is the correct way of enumerating through sub views to find text
fields?
The first method is the correct one. The second method will iterate over all the subviews, not just the subviews with type UITextField. The type in the for() is only a hint to the compiler.
For more information, see this question.
how do you 'convert' it from a view to a text field?
This is what typecasting is for.
for (UIView *view in [self.view subviews]) {
if ([view isKindOfClass:[UITextField class]]) {
// you don't need to cast just to add to the array
[mutableTFs addObject:view];
// typecasting works as it does in C
UITextField *textField = (UITextField *)view;
// do something with textField
}
}
The first method is the only working method of the two.
The second method would add all subviews to the array. That is if you would change subViews to subviews.
You could do the following:
for (UITextField *textField in [self.view subviews]) {
if ([textField isKindOfClass:[UITextField class]]) {
[mutableTFs addObject:textField];
}
}
That way you wouldn't have to convert the view to a text field to do something text field specific instead of just adding it to an array.
EDIT: If you don't want to convert to a text field right away, maybe because you're looking for both text fields and text views. This is how you'd convert it later:
for (UIView *view in [self.view subviews]) {
if ([view isKindOfClass:[UITextField class]]) {
UITextField *textField = (UITextField *)view;
// Do whatever you want with the text field.
}
if ([view isKindOfClass:[UITextView class]]) {
UITextView *textView = (UITextView *)view;
// Do whatever you want with the text view.
}
}
Here's the best way.
// Make sure you're releasing this!
NSMutableArray *textFields = [[NSMutableArray alloc] init];
for (UITextField *textField in [self.view subviews]) {
if ([textField isKindOfClass:[UITextField class]]) {
[textFields addObject:textField];
}
}
By specifying UITextField * as the type that you're performing the fast enumeration with, you'll be working with values that are casted already (by fast enumeration) from id to UITextField *. This does not guarantee that they are actually UITextFields though, so you still need a runtime check, in this case isKindOfClass:, to make sure the object you're currently working is really a UITextField.
So, both of them are correct, but only when combined.