Can I count up whole of views to check performance? - ios

Memory use and cpu time can be available like below.
Programmatically retrieve memory usage on iPhone
And also i'd like to know the number of views to check performance? Is it possible?

I solved myself. call countViewsInApp. thx.
+ (int)countViewsInApp {
int num = 0;
for (UIWindow *window in [[UIApplication sharedApplication] windows]) {
for (UIView *aView in [window subviews]) {
num++; // parent
num += [self countSubviews:aView];
}
}
return num;
}
+ (int)countSubviews:(UIView *)view {
int num = 0;
for (UIView *subview in [view subviews]) {
num++;
if ([[subview subviews] count] > 0) {
num += [self countSubviews:subview];
}
}
return num;
}

You can do like that:
- (NSUInteger *)countSubviewsInViewСonsideringView:(UIView *)view {
NSUInteger subviewsCount = 1; // the current view
for (UIView *subView in view.subviews) {
subviewsCount += [self countSubviewsInViewСonsideringView:subView]
}
return subviewsCount;
}
somewhere in code:
UIWindow* window = [[UIApplication sharedApplication] keyWindow];
NSUInteger count = [self countSubviewsInViewСonsideringView:window];

Related

Finding all UIButtons in subviews

I have a single UIButton in the view of my UIViewController. I also have ten more that are in a subview in the main view. I want to find all these buttons. So far I have:
-(void)findAllButtons{
for(UIView *view in self.view.subviews) {
if ([view isKindOfClass:[myButton class]]){
NSLog(#"found a button!");
}
}
}
It is only finding the single button though and not the other ten. Why is that? Shouldn't it iterate every single subview and then find them?
for (UIView *subView in scroll.subviews) {
if ([subView isKindOfClass:[UIButton class]]) {
UIButton *btn = (UIButton*)subView;
if (btn.tag == selectedButton.tag) {
btn.layer.borderWidth = 1.0f;
btn.layer.borderColor = [UIColor darkGrayColor].CGColor;
}else{
btn.layer.borderWidth = 1.0f;
btn.layer.borderColor = [UIColor clearColor].CGColor;
}
}
}
Just a few lines of code
-(void)findAllButtons {
[self findButtonsInSubviews:self.view.subviews];
}
- (void)findButtonsInSubviews:(NSArray *)subviews {
for(UIView *view in subviews) {
if ([view isKindOfClass:[UIButton class]]){
NSLog(#"found a button!");
} else {
[self findButtonsInSubviews:view.subviews];
}
}
}
A recursive function using Objective-C blocks like this will find all views of a given subclass type as specified in the test block in the view hierarchy of the given view:
NSMutableArray *marrAllButtons = [NSMutableArray new];
BOOL (^viewTest)(UIView*) = ^BOOL(UIView* viewToTest) {
return [view isKindOfClass:[UIButton class]];
};
void(^viewEnumerator)(UIView*) = ^(UIView* outerView){
for (UIView *view in outerView.subviews)
{
if (viewTest(view))
{
[marrAllButtons addObject:view];
}
else
{
viewEnumerator(view);
}
}
};
viewEnumerator(self.view);
NSLog(#"All Buttons %#", marrAllButtons);
- (NSMutableArray *)buttonsInView:(UIView *)view
{
NSArray *subviews = view.subviews;
NSMutableArray *buttons = [NSMutableArray array];
for (UIView *subview in subviews)
{
if ([subview isKindOfClass:[UILabel class]])
{
[buttons addObject:subview];
}
else if(subview.subviews)
{
[buttons addObjectsFromArray:[self buttonsInView:subview]];
}
}
return buttons;
}
Your method is right if all your button already have in self.view (main view).
Just set tag for all button to check and also make sure that all button are on the main view. I hope this will work.
-(void)findAllButtons{
for(UIView *view in self.view.subviews) {
if ([view isKindOfClass:[myButton class]]){
UIButton *button = (UIButton*)view;
NSLog(#"found a button with tag:%d",button.tag);
}
}
}
for(UIView * subView in view.subviews) // here write Name of you ScrollView.
{
// NSLog(#"test %#", [subView class]);
if([subView isKindOfClass:[UIButton class]])
{
UIButton *button = (UIButton*)subView;
[button setSelected:NO] ;
NSString *s1;
s1 = #",";
s1 = [s1 stringByAppendingString:[NSString stringWithFormat:#"%#",button.titleLabel.text ]];
s1 = [s1 stringByAppendingString:[NSString stringWithFormat:#"%#",#"," ]];
NSRange range = [temp_Colors_Name_comma rangeOfString:s1 ];
if(range.location == NSNotFound)
{
}
else
{
[button setSelected:YES];
}
}
}

iOS get Keyboard Window

So in iOS 7 I always got the Keyboard Window like this:
- (UIView *)keyboardView
{
UIWindow* tempWindow;
//Because we cant get access to the UIKeyboard throught the SDK we will just use UIView.
//UIKeyboard is a subclass of UIView anyways
UIView* keyboard;
NSLog(#"windows %d", [[[UIApplication sharedApplication]windows]count]);
//Check each window in our application
for(int c = 0; c < [[[UIApplication sharedApplication] windows] count]; c ++)
{
//Get a reference of the current window
tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:c];
//Get a reference of the current view
for(int i = 0; i < [tempWindow.subviews count]; i++)
{
keyboard = [tempWindow.subviews objectAtIndex:i];
NSLog(#"view: %#, on index: %d, class: %#", [keyboard description], i, [[tempWindow.subviews objectAtIndex:i] class]);
if([[keyboard description] hasPrefix:#"(lessThen)UIKeyboard"] == YES)
{
//If we get to this point, then our UIView "keyboard" is referencing our keyboard.
return keyboard;
}
}
for(UIView* potentialKeyboard in tempWindow.subviews)
// if the real keyboard-view is found, remember it.
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
if([[potentialKeyboard description] hasPrefix:#"<UILayoutContainerView"] == YES)
keyboard = potentialKeyboard;
}
else if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
if([[potentialKeyboard description] hasPrefix:#"<UIPeripheralHost"] == YES)
keyboard = potentialKeyboard;
}
else {
if([[potentialKeyboard description] hasPrefix:#"<UIKeyboard"] == YES)
keyboard = potentialKeyboard;
}
}
NSLog(#"view: %#, on index: %d", [keyboard description]);
return keyboard;
}
But in the iOS 8 beta 1 & 2 the window/view that is returned is the main App window. Any idea what is the the problem in my code? On my test device with iOS 7 it works great...
I also had this problem and found the solution.
Below is the code which will work for iOS 8.0 and also for below versions.
I have tested it on iOS 7 and 8.0 (Xcode Version 6.0.1)
- (void)addButtonToKeyboard
{
// create custom button
self.doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.doneButton.frame = CGRectMake(0, 163+44, 106, 53);
self.doneButton.adjustsImageWhenHighlighted = NO;
[self.doneButton setTag:67123];
[self.doneButton setImage:[UIImage imageNamed:#"doneup1.png"] forState:UIControlStateNormal];
[self.doneButton setImage:[UIImage imageNamed:#"donedown1.png"] forState:UIControlStateHighlighted];
[self.doneButton addTarget:self action:#selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];
// locate keyboard view
int windowCount = [[[UIApplication sharedApplication] windows] count];
if (windowCount < 2) {
return;
}
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
UIView* keyboard;
for(int i = 0 ; i < [tempWindow.subviews count] ; i++)
{
keyboard = [tempWindow.subviews objectAtIndex:i];
// keyboard found, add the button
if([[keyboard description] hasPrefix:#"<UIPeripheralHost"] == YES){
UIButton* searchbtn = (UIButton*)[keyboard viewWithTag:67123];
if (searchbtn == nil)//to avoid adding again and again as per my requirement (previous and next button on keyboard)
[keyboard addSubview:self.doneButton];
}//This code will work on iOS 8.0
else if([[keyboard description] hasPrefix:#"<UIInputSetContainerView"] == YES){
for(int i = 0 ; i < [keyboard.subviews count] ; i++)
{
UIView* hostkeyboard = [keyboard.subviews objectAtIndex:i];
if([[hostkeyboard description] hasPrefix:#"<UIInputSetHost"] == YES){
UIButton* donebtn = (UIButton*)[hostkeyboard viewWithTag:67123];
if (donebtn == nil)//to avoid adding again and again as per my requirement (previous and next button on keyboard)
[hostkeyboard addSubview:self.doneButton];
}
}
}
}
}
>
-(void) removedSearchButtonFromKeypad{
int windowCount = [[[UIApplication sharedApplication] windows] count];
if (windowCount < 2) {
return;
}
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
for(int i = 0 ; i < [tempWindow.subviews count] ; i++)
{
UIView* keyboard = [tempWindow.subviews objectAtIndex:i];
if([[keyboard description] hasPrefix:#"<UIPeripheralHost"] == YES){
[self removeButton:keyboard];
}else if([[keyboard description] hasPrefix:#"<UIInputSetContainerView"] == YES){
for(int i = 0 ; i < [keyboard.subviews count] ; i++)
{
UIView* hostkeyboard = [keyboard.subviews objectAtIndex:i];
if([[hostkeyboard description] hasPrefix:#"<UIInputSetHost"] == YES){
[self removeButton:hostkeyboard];
}
}
}
}
}
-(void) removeButton:(UIView*)keypadView{
UIButton* donebtn = (UIButton*)[keypadView viewWithTag:67123];
if(donebtn){
[donebtn removeFromSuperview];
donebtn = nil;
}
}
Hope this helps.
What i use to get the window for the keyboard is
- (void)findKeyboardWindow
{
for (UIWindow *window in [[UIApplication sharedApplication] windows])
{
if ([NSStringFromClass([window class]) isEqualToString:#"UITextEffectsWindow"])
{
_keyboardWindow = window;
break;
}
}
}
From my logs on iOS8 this contains one view
UIInputSetContainerView: 0x190d4430; frame = (0 0; 320 568); autoresize = W+H; layer = CALayer: 0x190d4630
Which contains another view
UIInputSetHostView: 0x190d4820; frame = (0 352; 320 216); layer = CALayer: 0x190d49c0
Since those dimensions are 216.0f height i guess that is the keyboard.
Was this what you where looking for?
On iOS 13, we can get keyboard window like this:
let windows = UIApplication.shared.windows
if let keyboardWindow = windows.first(where: { NSStringFromClass($0.classForCoder) == "UIRemoteKeyboardWindow" }) {
return
}
In the first iOS 8 beta, the system keyboard is the UIInputSetHostView subview of the UIInputSetContainerView subview of one of the application windows.

How to remove top bar of the keyboard in ios 7

In my program I use UIWebView with editable div for the Rich text editor. I need to remove top bar of the keyboard.
I used below code - it removes only next/previous buttons
I want to remove full top bar.
- (void)removeBar {
UIWindow *keyboardWindow = nil;
for (UIWindow *testWindow in [[UIApplication sharedApplication] windows]) {
if (![[testWindow class] isEqual:[UIWindow class]]) {
keyboardWindow = testWindow;
break;
}
}
for (UIView *possibleFormView in [keyboardWindow subviews]) {
// iOS 5 sticks the UIWebFormView inside a UIPeripheralHostView.
if ([[possibleFormView description] rangeOfString:#"UIPeripheralHostView"].location != NSNotFound) {
for (UIView *subviewWhichIsPossibleFormView in [possibleFormView subviews]) {
if ([[subviewWhichIsPossibleFormView description] rangeOfString:#"UIWebFormAccessory"].location != NSNotFound) {
[subviewWhichIsPossibleFormView removeFromSuperview];
}
}
}
}
}
I found the solution for iOS 8. You can check it here: [ iOS 8 - Remove Previous/Next/Done UIKeyboard Toolbar inside a UIWebView][1]
You may try and improve this. try call this function inside Your UIKeyboardDidShowNotification event handler.
Hope this helps...
This is the level of views in accessory:
(UIWebFormAccessory) -> (UIToolbar) -> (UIImageView,UIToolbarButton,UIToolbarButton)
-(void) removeKeyboard {
UIWindow *keyboardWindow = nil;
for (UIWindow *testWindow in [[UIApplication sharedApplication] windows]) {
if (![[testWindow class] isEqual : [UIWindow class]]) {
keyboardWindow = testWindow;
break;
}
}
// Locate UIWebFormView.
for (UIView *possibleFormView in [keyboardWindow subviews]) {
if ([[possibleFormView description] hasPrefix : #"<UIInputSetContainerView"]) {
for (UIView* peripheralView in possibleFormView.subviews) {
for (UIView* peripheralView_sub in peripheralView.subviews) {
// hides the backdrop (iOS 8)
if ([[peripheralView_sub description] hasPrefix : #"<UIKBInputBackdropView"] && peripheralView_sub.frame.size.height == 44) {
[[peripheralView_sub layer] setOpacity : 0.0];
}
// hides the accessory bar
if ([[peripheralView_sub description] hasPrefix : #"<UIWebFormAccessory"]) {
for (UIView* UIInputViewContent_sub in peripheralView_sub.subviews) {
CGRect frame1 = UIInputViewContent_sub.frame;
frame1.size.height = 0;
peripheralView_sub.frame = frame1;
UIInputViewContent_sub.frame = frame1;
[[peripheralView_sub layer] setOpacity : 0.0];
}
CGRect viewBounds = peripheralView_sub.frame;
viewBounds.size.height = 0;
peripheralView_sub.frame = viewBounds;
}
}
}
}
}
}
There is a solution for this here.
That answer includes a function called removeKeyboardTopBar which should be called on the UIKeyboardWillShowNotification.
Listen to the notification:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
On the notification, call the function after a delay. The delay is necessary to make sure that the objects exist before the function runs (since we are calling this on keyboard will show, not keyboard did show).
- (void)keyboardWillShow:(NSNotification*) notification {
[self performSelector:#selector(removeKeyboardTopBar) withObject:nil afterDelay:0];
}
From what I can tell, it looks like the keyboard has an added inputAccessoryView to the top of it. Otherwise, I'm not certain why it is appearing. Anyways, if that is the case then whatever you assign firstResponder to set their inputAccessoryView to nil:
someObjectWithFirstResponder.inputAccessoryView = nil;
EDIT: Looks like the UIWebView sets the inputAccessoryView itself, but if it is actually receiving firstResponder then you should be able to override the inputAccessoryView. If setting it to nil causing it to default back to the inputAccessoryView that you are trying to remove, then you might try setting it to an empty view:
someObjectWithFirstResponder.inputAccessoryView = [UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
EDIT This method does not work in iOS 8.0 and greater.

iOS keyboard style in webview?

I am pretty new to web development, my project is using Jquery-mobile, Phonegap and compass (scss).
By default, the keyboard showing up when an input field gets focus, is a keyboard containing a sort of top bar with fields navigation abilities. I would like to get rid of this navigation bar, and display the simplest keyboard available in iOS. I tried enclosing my objects in a form or without a form tag, without success. I don't have any clue how to achieve this, nore if it is possible :/
Thx for any advice!
If you hit this problem, make sure to head over to https://bugreport.apple.com and duplicate rdar://9844216
To remove the bar you should dive into the objective-c code.
This is the code that I use: (inside the controller that contains your UIWebview add the following code)
First add an observer to receive the notification for the keyboard :
-(void)viewWillAppear:(BOOL)animated{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
}
- (void)keyboardWillShow:(NSNotification *)note {
[self performSelector:#selector(removeBar) withObject:nil afterDelay:0];
}
- (void)removeBar {
// Locate non-UIWindow.
UIWindow *keyboardWindow = nil;
for (UIWindow *testWindow in [[UIApplication sharedApplication] windows]) {
if (![[testWindow class] isEqual:[UIWindow class]]) {
keyboardWindow = testWindow;
break;
}
}
// Locate UIWebFormView.
for (UIView *formView in [keyboardWindow subviews]) {
// iOS 5 sticks the UIWebFormView inside a UIPeripheralHostView.
if ([[formView description] rangeOfString:#"UIPeripheralHostView"].location != NSNotFound) {
for (UIView *subView in [formView subviews]) {
if ([[subView description] rangeOfString:#"UIWebFormAccessory"].location != NSNotFound) {
// remove the input accessory view
[subView removeFromSuperview];
}
else if([[subView description] rangeOfString:#"UIImageView"].location != NSNotFound){
// remove the line above the input accessory view (changing the frame)
[subView setFrame:CGRectZero];
}
}
}
}
}
I was working on iOS 7.0 and fixed it with the following code on UIWebView
-(void)removeExtraBar
{
UIWindow *keyboardWindow = nil;
for (UIWindow *testWindow in [[UIApplication sharedApplication] windows]) {
if (![[testWindow class] isEqual:[UIWindow class]]) {
keyboardWindow = testWindow;
break;
}
}
for (UIView *possibleFormView in [keyboardWindow subviews]) {
if ([[possibleFormView description] rangeOfString:#"UIPeripheralHostView"].location != NSNotFound) {
for (UIView *subviewWhichIsPossibleFormView in [possibleFormView subviews]) {
if ([[subviewWhichIsPossibleFormView description] rangeOfString:#"UIWebFormAccessory"].location != NSNotFound) {
[subviewWhichIsPossibleFormView removeFromSuperview];
}
else if([[subviewWhichIsPossibleFormView description] rangeOfString:#"UIKBInputBackdropView"].location != NSNotFound) {
[subviewWhichIsPossibleFormView removeFromSuperview];
}
}
}
}
}
This works for me with the UIWebView keyboard issue on iOS 7.0

How to find my specific button over window?

I have created by custom UIButton for iPad application.
Now for specific reasons, I want to hide those buttons. To do that I am using following code:
NSMutableArray *viewArray = [[NSMutableArray alloc] init];
// Get all the windows
for (UIWindow *window in [[UIApplication sharedApplication] windows])
{
// check for main screen
if (![window respondsToSelector:#selector(screen)] ||
[window screen] == [UIScreen mainScreen])
{
// check for all the subviews available in window
for (UIView * view in [window subviews]) {
// check whether it supports this method
if ([view respondsToSelector:#selector(isKindOfClass:)]) {
// type cast to button
// UIButton *btn = (UIButton *)view;
// if its type of my custom button class
if ([[view class] isKindOfClass:[DINNextLTProBoldButton class]]) {
// hide view and add to array
[view setHidden:YES];
[viewArray addObject:view];
}
}
}
}
}
But I am not able to get my custom buttons in this array. It stays empty only even thought the view which is being appeared on window/screen have those buttons.
Where I am going wrong? Please guide me.
try this (untested, but it gives the basic idea)
- (void)hideViewOfClass:(Class)clazz inHeirarchyOfView:(UIView *)view
{
for (UIView *subview in [view subviews])
{
if ([subview isKindOfClass:clazz])
subview.hidden = YES;
else
[self hideViewOfClass:clazz inHeirarchyOfView:subview];
}
}
call this function at first like:
for (UIWindow *window in [[UIApplication sharedApplication] windows])
[self hideViewOfClass:[DINNextLTProBoldButton class] inHeirarchyOfView:window];

Resources