Is there anyway to remove a view from the uiapplication window of a particular class?
I have a class called MainView that is the top header bar for the app on the main screen. I have a view that gets added after that where users can swipe up and down to different screens but on the screen where a user swipes up or down too the MainView bar at the top needs to be removed. How should I go about this? Should I try removing this MainView class from the UIApplication?
[yourSubview removeFromSuperview];
If your MainView instance is added directly to the window, you can get the array of the window's subviews. Then iterate over the subviews until you find a view that is type MainView and remove it from the window.
NSArray *subviews = [UIApplication sharedApplication].delegate.window.subviews;
for (UIView *view in subviews)
{
if ([view isKindOfClass:[MainView class]])
{
[view removeFromSuperview];
break;
}
}
Related
In my first ViewController having property
#property (nonatomic, strong) UIView* snapShotView;
And I call
self.snapShotView = [[UIScreen mainScreen] snapshotViewAfterScreenUpdates:YES];
[self.view addSubview: self.snapShotView];
Then I present two viewcontrollers on top of it and dismiss them again. When I am back on the first viewController the snapShotView property is empty, but the view still displays. I want to remove the snapShotView, but I no longer have a reference.
If I only display one ViewController on top of the first ViewController and dismiss it, the property still references the correct view. If I present and dismiss another view on top of it, the property is null once the additional two ViewControllers are dismissed.
How can I reference and remove the snapshoView?
It is really strange that you are loosing the reference to subView, but anyway if you are loosing the reference to subView and because of that if you are not able to remove the subView you added and if that's your problem, I'll say you don't need the reference to remove the subView from view :)
You can do something like this :)
[[self.view subviews] makeObjectsPerformSelector:#selector(removeFromSuperview)];
This statement will remove all the subViews from your view without reference :)
and in case you have multiple views added to your view and want to remove just a specific view make use of tags :)
self.snapShotView = [[UIScreen mainScreen] snapshotViewAfterScreenUpdates:YES];
self.snapShotView.tag = someValue;
[self.view addSubview: self.snapShotView];
and while removing
for (UIView *view in [self.view subviews]) {
if (view.tag == someValue) {
[view removeFromSuperview];
}
}
Any idea how to get an inputAccessoryView to anchor to the tab bar rather than the bottom of the screen?
I have created a UIViewController and overridden the following methods:
-(BOOL)canBecomeFirstResponder {
return YES;
}
-(UIView *)inputAccessoryView {
CGRect frame = CGRectMake(0, 0, self.view.frame.size.width, 44);
self.keyboardInputAccessoryView =[[BRKeyboardInputBarView alloc] initWithFrame:frame leftButtonTitle:#"Left" andRightButtonTitle:#"Send"];
[self.keyboardInputAccessoryView setDelegate:self];
[self.keyboardInputAccessoryView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.keyboardInputAccessoryView removeFromSuperview];
return self.keyboardInputAccessoryView;
}
View controller with inputAccessoryView covering the tab bar
By the looks of it the view controller adds the view to the window rather than the current view controllers view, which would explain its positioning. However if I remove the line:
[self.keyboardInputAccessoryView removeFromSuperview];
I get a crash when I tap in the textview of my accessory view:
The view hierarchy is not prepared for the constraint:<NSLayoutConstraint:0x7fa0c2ca5f80 BRKeyboardInputBarView:0x7fa0c2d6fad0.bottom == UIInputSetContainerView:0x7fa0c295a2c0.bottom>
So I guess what I am asking is what is the correct way to add a keyboard accessory view so that it plays nicely with auto layout and avoids the crash, but also anchors itself to the view and not the window?
What you are seeing is the right behaviour.
The results you are seeing is because of the fact that UIViewController is a UIResponder subclass. By overriding the inputAccessoryView and returning an instance of a view, UIViewController will take care of placing that view at the bottom of the screen and animating it appropriately when keyboard appears or disappears.
If you want to add this bar on top of your keyboard, then you need to set the property inputAccessoryView of a textField/textView to your custom view.
I've looked through various SO questions on the topic and I have not found a solution. I have a UIViewController with a UITableView and a UICollectionView. I want the UICollectionView to scroll to the top, when the user taps it.
The documents say if you have more than one UiScrollView subclass - you need to set them to no and the UiScrollView you want to scroll to the top, to yes.
So I wrote this bit of code to go through all my views:
for (UIScrollView *view in self.view.subviews) {
if ([view isKindOfClass:[UIScrollView class]]) {
view.scrollsToTop = NO;
}
}
self.collectionView.scrollsToTop = YES;
This way I am sure any subclass of UiScrollView has it's scrollsToTop property set to no.
However tapping on the status bar does not do anything.
Can someone tell me what I am missing here?
Thank you
It seems that you are only iterating through the subviews of your main view. Your UITableView may be nested inside another view. Try doing the following;
//in view did load
[self setScrollToTopFalse:self.view];
self.collectionView.scrollsToTop = YES;
-(void)setScrollToTopFalse:(UIView *)v
{
for (UIView * v1 in [v subviews]) {
if ([[v1 class]isSubclassOfClass:[UIScrollView class]]) {
((UIScrollView *)v1).scrollsToTop = NO;
}
[self setScrollToTopFalse:v1];
}
}
I want to add UISearchbar with two scoop button to my UIView. i do not want to either navigation or table view.
when user open model view page from sheet comes up with Search option and rest. when user click on Search button scoop button to be visible.
i tried to do but i am not getting desire result.
how to remove Background of scope button, i have removed uisearchbar, similar way i do not want white background for button. is it possible. showsScopeBar i am making FALSE still background can be visible.
To remove Background image of scope buttons just try :
Class segmentedControlBackgroundView = NSClassFromString(#"_UISegmentedControlBackgroundView");
for(UIView *view in self.searchBar.subviews) {
if([view isKindOfClass:[UISegmentedControl class]]){
for(UIView *subview in view.subviews) {
if ([subview isKindOfClass:segmentedControlBackgroundView]) {
subview.hidden = YES;
}
}
}
}
I have created an iPad app. In the middle of the app, under a button I have added a split view. After the button is clicked the split is shown successfully. But in the split view I want to add a button. When the button is tapped then I want to remove the split view and show another xib in whole screen. How can I do that?
First I had some questions,
if you able to add a Splitview on top of a viewcontroller with its button click?
In the above case do you get orientation in splitview controllers?
While I try to do I had this two problems.
Then What I do is, On a button click to show the splitview I called UIApplication Primary window, remove all current views in window and Add the new controllers view.
Do the Same as when need to go back.
See the following code,
- (void) displayControllerViewStack: (UIView *)view {
NSArray *subViews = [self.window subviews];
while ([subViews count] > 0 ) {
// Need to clear out the list, as only view at index 0 gets rotation notification.
[(UIView *)[subViews objectAtIndex:0] removeFromSuperview];
subViews = [self.window subviews];
}
[self.window addSubview: view];
}
thanks,
Naveen Shan