how can i programmatically show/hide this opaque view from UISearchDisplayController?
Probably in searchDisplayControllerWillBeginSearch or searchDisplayControllerDidBeginSearch i need to set something... but what?
thanks.
Temporary solved using UIKeyboardWillAppearNotification.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow) name:UIKeyboardWillShowNotification object:nil];
OpaqueView is an UIControl with alpha = 0.8.
- (void) keyboardWillShow {
for( UIView *subview in self.view.subviews ) {
if( [subview isKindOfClass:[UIControl class]] ) {
UIControl *v = (UIControl*)subview;
if (v.alpha < 1) {
v.hidden = YES;
}
}
}
}
I used this ORRIBLE way to temporary fix problem.... any other idea will be appreciated!
thanks.
Code given by elpsk is current but will not work in iOS7 and above
Code working in both iOS6 and iOS7 is as below
- add below notification in viewDidLoad
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow) name:UIKeyboardWillShowNotification object:nil];
Write below function
- (void) keyboardWillShow {
for( UIView *subview in self.view.subviews ) {
if([subview isMemberOfClass:[UIControl class]] ||
([[[subview class] description] isEqualToString:#"UISearchDisplayControllerContainerView"])) {
UIControl *v = (UIControl*)subview;
if (v.alpha < 1) {
v.hidden = YES;
}
}
}
}
NOTE : Code just have one extra condition as in iOS7 UIControl class become UISearchDisplayControllerContainerView,
The other answers where not working for me. This one works for me on iOS7 and iOS8.
for( UIView *subview in self.view.subviews ) {
if([subview isMemberOfClass:[UIControl class]] ||
([[[subview class] description] isEqualToString:#"UISearchDisplayControllerContainerView"])) {
for(UIView *subView2 in subview.subviews)
{
for(UIView *subView3 in subView2.subviews)
{
if (subView3.alpha < 1) {
subView3.hidden = YES;
}
}
}
}
}
If you don't need support for iOS7 please don't use the searchDisplayController anymore because its deprecated. For iOS8 use the UISearchController and the dimsBackgroundDuringPresentation
Property
Ref: https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UISearchController/index.html#//apple_ref/occ/instp/UISearchController/dimsBackgroundDuringPresentation
Mmmm...quick answer. Not pretty but surely works
#pragma mark UISearchBarDelegate
// Displays a view to simulate the lose of focus
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
searchBar.showsCancelButton = NO;
searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
UIButton *view1 = [[UIButton alloc] init];
view1.frame = CGRectMake(0, 0, 320, MAX(480, self.tableView.contentSize.height));
view1.alpha = 0.6;
view1.tag = 2000;
view1.backgroundColor = [UIColor blackColor];
[view1 addTarget:self
action:#selector(removeView)
forControlEvents:UIControlEventTouchUpInside];
[self.tableView setScrollEnabled:NO];
[self.tableView addSubview:view1];
[view1 release];
}
/**
* Pop the view and the keyboard
*/
- (void)removeView {
UIView *v = [self.tableView viewWithTag:2000];
v.hidden = YES;
[v removeFromSuperview];
[self.tableView setScrollEnabled:YES];
[self.searchBar resignFirstResponder];
}
That view is showed when you're writing, so I guess you should use it at searchBarTextDidBeginEditing. If I'm wrong, use it when you start searching or whatever.
Related
I would like to customize the delete button which is shown when performing the 'swipe to left'-action on a tableview cell. I currently set up a subclass of a UITableViewCell but also want to customize the delete-button which is being shown.
My goal is to place three buttons when swiping.
I choose for another implementation where I was using a UIScrollview in each cell.
http://www.teehanlax.com/blog/reproducing-the-ios-7-mail-apps-interface/
Accepted answer will not work on iOS 7, as there is now UITableViewCellContentView in between. So subviews loop now should look like this(if you want to support older iOS versions too, use currently accepted answer for iOS 6.1-)
for (UIView *subview in self.subviews) {
for (UIView *subview2 in subview.subviews) {
if ([NSStringFromClass([subview2 class]) rangeOfString:#"Delete"].location != NSNotFound) {
// Do whatever you want here
}
}
}
This might help you.
- (void)willTransitionToState:(UITableViewCellStateMask)state
{
[super willTransitionToState:state];
if ((state & UITableViewCellStateShowingDeleteConfirmationMask) == UITableViewCellStateShowingDeleteConfirmationMask)
{
for (UIView *subview in self.subviews)
{
if ([NSStringFromClass([subview class]) isEqualToString:#"UITableViewCellDeleteConfirmationControl"])
{
UIImageView *deleteBtn = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 64, 33)];
[deleteBtn setImage:[UIImage imageNamed:#"arrow_left_s11.png"]];
[[subview.subviews objectAtIndex:0] addSubview:deleteBtn];
}
}
}
}
Referenced from:
Customize the delete button in UITableView
create custom delete button for uitableview
Custom Delete button On Editing in UITableView Cell
-(void)willTransitionToState:(UITableViewCellStateMask)state{
NSLog(#"EventTableCell willTransitionToState");
[super willTransitionToState:state];
if((state & UITableViewCellStateShowingDeleteConfirmationMask) == UITableViewCellStateShowingDeleteConfirmationMask)
{
UIImageView *deleteBtn = [[UIImageView alloc]initWithFrame:CGRectMake( 320,0, 228, 66)];
[deleteBtn setImage:[UIImage imageNamed:#"BtnDeleteRow.png"]];
[[self.subviews objectAtIndex:0] addSubview:deleteBtn];
[self recurseAndReplaceSubViewIfDeleteConfirmationControl:self.subviews];
[self performSelector:#selector(recurseAndReplaceSubViewIfDeleteConfirmationControl:) withObject:self.subviews afterDelay:0];
}
}
The solutions above didn't work for me for iOS 7, at - (void)willTransitionToState:, the delete button wasn't in the view heirarchy so I wasn't able to manipulate anything. I ended up doing everything on - (void)didTransitionToState:. The example below was specifically for when my cells had some spacing at the top so I'm altering the frame of the delete button. If you want to customize the delete button, you can just add a view on top of the delete button or replace it with your own UIButton
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
//your own stuff
//for some reason, editingAccessoryView cannot be nil and must have a non-CGRectZero frame
self.editingAccessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
}
return self;
}
- (void)didTransitionToState:(UITableViewCellStateMask)state
{
[super didTransitionToState:state];
if ((state & UITableViewCellStateShowingDeleteConfirmationMask) == UITableViewCellStateShowingDeleteConfirmationMask)
{
UIView *deleteButton = [self deleteButtonSubview:self];
if (deleteButton) {
CGRect frame = deleteButton.frame;
frame.origin.y += defined_padding;
frame.size.height -= defined_padding;
deleteButton.frame = frame;
}
}
}
- (UIView *)deleteButtonSubview:(UIView *)view
{
if ([NSStringFromClass([view class]) rangeOfString:#"Delete"].location != NSNotFound) {
return view;
}
for (UIView *subview in view.subviews) {
UIView *deleteButton = [self deleteButtonSubview:subview];
if (deleteButton) {
return deleteButton;
}
}
return nil;
}
I have a little problem with the notific of keyboard disappear, I add the observer:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardDidDisappear:)
name:UIKeyboardWillHideNotification
object:nil];
The add the method:
- (void) keyboardDidDisappear:(NSNotification *)notification {
NSLog(#"disappear");
}
In my application I have some button, on click it show two uitextfield with this method:
- (void) showFieldStoryView:(id)sender {
if (storyContentView.hidden == NO) {
UIButton *button = (UIButton *)sender;
buttonTag = [button tag];
int indexArray = buttonTag - 1;
NSMutableDictionary *dict = [arrayPunteggi objectAtIndex:indexArray];
[fieldUno removeFromSuperview];
[fieldDue removeFromSuperview];
[fieldUno setHidden:YES];
[fieldDue setHidden:YES];
[storyContentView addSubview:fieldUno];
[storyContentView addSubview:fieldDue];
[fieldUnoAccusi removeFromSuperview];
[fieldDueAccusi removeFromSuperview];
[fieldUnoAccusi setHidden:YES];
[fieldDueAccusi setHidden:YES];
[labelPunti removeFromSuperview];
[labelAccusi removeFromSuperview];
[labelPunti setHidden:YES];
[labelAccusi setHidden:YES];
[storyContentView addSubview:labelPunti];
[storyContentView addSubview:labelAccusi];
if (fieldUno.isFirstResponder) {
point = CGPointMake(0,button.frame.origin.y);
}
else {
[fieldUno becomeFirstResponder];
}
}
}
the problem is that every time I click on this button I see on console the log disappear, and the strange thins is that the keyboard is always show oh the screen, where is the problem?
I've this code:
int i=0;
for (UIView *view in [self subviews]) {
//NSLog(#"index %d - i %d",index,i);
if (i==index) {
NSLog(#"index %d - i %d",index,i);
[view setAlpha:0.3];
[view setUserInteractionEnabled:NO];
}
i++;
}
"index" is a variable used to select the view to modify.
The problem is that nothing happen, instead if I run this code:
for (UIView *view in [self subviews]) {
[view setAlpha:0.3];
[view setUserInteractionEnabled:NO];
}
every view will be modify obviously, but i need the first code :)
ideas?
Set the tag of all the subviews inside your scroll view and then pass the tag of selected view to the index variable hope it is an integer.
for(int i = 1; i <=4; i++){
UIView *myView = [UIView......]; // set your view
myView.tag = i;
[your_scrollView addSubview:myView];
}
then you can do like following.
for (UIView *view in [your_scrollView subviews])
{
if (view.tag == index)
{
[view setAlpha:0.3];
[view setUserInteractionEnabled:NO];
}
}
or there is an easy way to get the view [your_scrollView viewWithTag:index]
Try this code :-
for (UIView *view in [scrollView subviews]) {
[view setAlpha:0.3];
[view setUserInteractionEnabled:NO];
}
Hope it works
I need to know how to stop scrolling in a UIwebview in xcode 4?
[[[webView subviews] lastobject] setScrollingEnabled:NO];
The above code does not work because "NSarray" for instance message does not declare a method with selector 'lastobject'. Why is this or is there new code I am unaware of to disable scrolling? Thanks.
UIView* row = nil;
for(row in webView.subviews){
if([row isKindOfClass:[UIScrollView class] ]){
UIScrollView* scrollRow = (UIScrollView*) row;
scrollRow.scrollEnabled = NO;
scrollRow.bounces = NO;
scrollRow.backgroundColor=[UIColor clearColor];
}
}
your code is trying to make assumptions about the order of the subviews that Apple is defining..
Note that with iOS 5 you have direct access to the scroll view.
So if you wanted to maintain compatibility you could do this:
UIScrollView *scrollView = nil;
if ([webView respondsToSelector:#selector(scrollView)]) { //iOS 5+
scrollView = webView.scrollView;
} else { //iOS 4-
for(UIView *view in webView.subviews){
if([view isKindOfClass:[UIScrollView class] ]){
scrollView = (UIScrollView *) view;
break;
}
}
}
scrollView.scrollEnabled = NO;
scrollView.bounces = NO;
scrollView.backgroundColor=[UIColor clearColor];
Simply add this line to viewDidLoad:
myWebView.scrollView.scrollEnabled = NO;
I would set the webview's UserInteractionEnabled property to NO so that it cannot be scrolled.
In my application I have to add a search bar at the head of the tableview. I am able to add the searchbar but problem is without adding default search bar of ios can i add my customize search bar?? I am giving an image to see what types of search bar will be there...
you can subclass the UISearchBar and override the layoutSubviews method :
- (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:#"yourImage.png"] ];
[searchField setBorderStyle:UITextBorderStyleNone];
}
[super layoutSubviews];
}
Also you can :
//to clear searchbar backgraound
- (void) clearSearchBarBg
{
for (UIView *subview in theSearchBar.subviews)
{
if ([subview isKindOfClass:NSClassFromString(#"UISearchBarBackground")])
{
[subview removeFromSuperview];
break;
}
}
}
//display showSearchButtonInitially in a keyboard
- (void)showSearchButtonInitially
{
UIView * subview;
NSArray * subviews = [theSearchBar subviews];
for(subview in subviews)
{
if( [subview isKindOfClass:[UITextField class]] )
{
NSLog(#"setEnablesReturnKeyAutomatically");
[((UITextField*)subview) setEnablesReturnKeyAutomatically:NO];
((UITextField*)subview).delegate=self;
[((UITextField*)subview) setEnabled:TRUE];
((UITextField*)subview).borderStyle = UITextBorderStyleNone;
break;
}
}
}
Look for Apple DOC for UISearchBar
You have bunch of methods there to get whatever you want
You can get UITextView Inside the search bar by
UITextField *textField = [searchBar.subviews objectAtIndex:2];
if ([textField isKindOfClass:[UITextField class]]) {
//Do your customization
}
Again look for AppleDoc for UITextField. You have bunch of methods for that also.
Yeah definitely. You can make your custom search bar (which is a sub-class of UIView) and add it as subview to the tableHeaderView.
[[searchBarDesign.subviews objectAtIndex:0] removeFromSuperview];
here searchBarDesign is my searchBar name.
I think it's better just set all properties of UISearchBar when it is loaded.
#interface MySearchBar : UISearchBar
#end
#implementation MySearchBar
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self myInitialize];
}
return self;
}
-(void)awakeFromNib
{
[super awakeFromNib];
[self myInitialize];
}
-(void)myInitialize
{
self.backgroundImage = [UIImage imageNamed:#"image.png"];
for (UIView* subview in self.subviews) {
if ([subview isKindOfClass:[UITextField class]]) {
//customize text field
UITextField* textfield = (UITextField*) subview;
}
}
}
#end