Hi To remove SubViews from UINavigation bar i am using following code.I have label,button and imageVIew as subView in navigation bar.
for (UIView *view in self.navigationController.navigationBar.subviews) {
[view removeFromSuperview];
}
While i am running this it is removing the backGround image of the navigationBar which i added as
[self.navigationController.navigationBar setBackgroundImage:[UIImagem imageNamed:#"header-background"] forBarMetrics:UIBarMetricsDefault];
after removing subViews i am adding the background agin,But it is not adding.
Is there any way to remove only subViews of navigation bar without removing the background.
a fast option is to add a value to the tag property of the views you want to remove and check for it before removing the the subview, for example, assuming that you add a non-zero value to your subviews:
for (UIView *view in self.navigationController.navigationBar.subviews) {
if (view.tag != 0) {
[view removeFromSuperview];
}
}
Try this,
for (UIView *view in self.navigationController.navigationBar.subviews) {
if([view isKindOfClass:[UIImageView class]])
{
//change your bar image
}
else
{
[view removeFromSuperview];
}
}
Related
I'd like to know what makes the backgrounds of subviews (labels for example) of a UITableViewCell become transparent while selected/highlighted. I need to avoid that behaviour for some subviews of my content view. I tried overriding the setSelected / setHighlighted methods with some success, but that transparency I wasn't able to reproduce. Any thoughts?
I think I got a somewhat close behaviour using the following piece of code
- (void) setView:(UIView*) view asHighlighted:(BOOL) highlighted {
if([view isKindOfClass:[UILabel class]]){
UILabel* label = (UILabel*) view;
[label setHighlighted:highlighted];
}
else if([view isKindOfClass:[UIImageView class]]){
UIImageView* imageView = (UIImageView*) view;
[imageView setHighlighted:highlighted];
}
if(highlighted){
[view.undoManager registerUndoWithTarget:view
selector:#selector(setBackgroundColor:)
object:view.backgroundColor];
view.backgroundColor = [UIColor clearColor];
}
else {
[view.undoManager undo];
}
for(UIView* subview in view.subviews){
[self setView:subview asHighlighted:highlighted];
}
}
Not sure this is the right way to use UndoManager but it works. You can use this in both setSelected and setHighlighted to mimic selection behaviour of UITableViewCells
In my application i have a view
in that i have many UIElements, buttons, etc..
I need to set
UserInteractionEnabled:NO for all the ui elements in that view except one button.
I tried with
[self.view setUserInteractionEnabled:NO];
The Require button also the subview that self.view that button also apply same behavior.
I can able to apply individually but it is not a good way.
How should i set UserInteractionEnabled:NO for all the other ui elements except one button
for (UIView *view in [self.view subviews])
{
if (view.tag==101)
[ view setUserInteractionEnabled:YES];
else
[ view setUserInteractionEnabled:NO];
}
Check this out:
for (UIView *view in self.view.subviews) {
if (!([view class]==[UIButton class]) )
{
view.userInteractionEnabled = NO;
}
}
You can just add transparent subview in the front of your view and place button on this transparent view:
UIView* maskView = [[UIView alloc] initWithFrame:self.view.bounds];
maskView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:maskView];
[maskView addSubview:buttonView];
And be sure that this transparent view is the last added subview, or just push it to the front of view in viewWillAppear:
[self.view bringSubviewToFront:maskView];
How can I remove all buttons from a view using iOS 7?
Here's code that works in earlier versions of iOS:
for(UIView *view in cell.subviews){
if([view isMemberOfClass:[UIButton class]]){
[(UIButton *)view removeFromSuperview];
}
}
First you need to get all subviews from view and then check all view is type of UIButton.For more info see this...
for (UIView *view in self.view.subviews)
{
if ([view isMemberOfClass:[UIButton class]])
{
[(UIButton *)view removeFromSuperview];
}
}
It looks like you're simply not doing a loop through the subviews. Assuming this is a view controller:
NSArray * allSubviews = [self.view subviews];
for(UIView view in allSubviews)
{
if([view isMemberOfClass:[UIButton class]])
{
[view removeFromSuperview];
}
}
You also don't need to cast "view" to "UIButton *" here because the base class of "UIView" is what implements "removeFromSuperview".
Sorry for late reply. Actually this part of code will work until iOS 6.0, but in iOS 7.0 and onwards, the code is not working.
So finally I found the solution to remove all subview from UIScrollview with the following statement.
[scrollView.subviews makeObjectsPerformSelector:#selector(removeFromSuperview)].
Try, this will work
for(UIView *view in self.view.subviews)
{
if ([view isKindOfClass:[UIButton class]])
{
[view removeFromSuperview];
}
}
I am currently at topViewController while a UIView is added to my rootViewController. Is there any way that I can remove all UIViews before going to rootViewController via popToRootViewContoller?
In Going back action,type following code:
for (UIView *subview in self.view.subviews) {
[subview removeFromSuperview];
}
And then type popToRootViewContoller.
Add this in viewWillAppear: method of YourViewController
for (UIView *view in self.view.subviews) {
[view removeFromSuperview];
}
Hope it helps you
In viewWillDisappear you can do this task, when you are leaving your rootViewController then this method will call , so you can remove your view at this point.
-(void)viewWillDisappear:(BOOL)animated
{
for (UIView *view in [self.view subviews]) {
[view removeFromSuperview];
}
}
Try this:
-(void)viewWillAppear:(BOOL)animated
{
NSArray *subviews = [self.view subviews];
for (UIView *subview in subviews) {
[subview removeFromSuperview];
}
}
[yourView removeFromSuperView]
but added subviews willnot be there when you pop a UIViewController, so i don't think you need to do this
I have a normal Interace in interface builder (there's a UILabel and a UIButton). In code, I create a UIScrollView with a (unknown) number of UILabels (depends by the User). These labels are created in - (void)viewWillAppear to make sure the data is up to date. To remove the labels, in viewWillDisappear i'm calling
for(UIView *subview in [scrollView subviews])
{
[subview removeFromSuperview];
}
The problem is, after the view get's called again, the Objects created with interfacebuilder (the UILabel & UIButton) disappear. Do the get removed by calling [subview removeFromSuperview];? If yes, how to add them again? Everything which is created by code is still thereā¦
you should add tag to your labels that you add dynamically.
for(int i = 0; i < n; i++){
UILabel *label = [UILabel alloc...];
label.tag = 999999;
}
then.
for(UIView *subview in [scrollView subviews])
{
if([subview isKindOfClass:[UILabel class]] && subview.tag == 999999){
[subview removeFromSuperview];
}
}
Don't remove all subviews. Remove only the labels you added in viewWillAppear.
UILabel, UIButton and UITextfields are basically subclasses of UIView.
So if you remove all the UIViews under the scrollview, it will also remove them too.
for (UIView *myView in [scrollView subviews]) {
if([myView isKindOfClass:[UILabel class]]){
[myView removeFromSuperview];
}
if([myView isKindOfClass:[UIButton class]]){
[myView removeFromSuperview];
}
if([myView isKindOfClass:[UITextField class]]){
[myView removeFromSuperview];
}
}