ObjectiveC - How to remove same previous UIView before create new - ios

I am creating a view on UITableView here is the code
UIView *olderLinesView = (UIView*)[self.view viewWithTag:1820];
for (UIView* view in [olderLinesView subviews]) {
[view removeFromSuperview];
}
[olderLinesView removeFromSuperview];
UIView *linesView=[[UIView alloc]initWithFrame:CGRectMake(2, -2, 1022, height)];
[linesView setBackgroundColor:[UIColor clearColor]];
[linesView setUserInteractionEnabled:NO];
[linesView setTag:1820];
[tableView addSubview:linesView];
But the problem is if i remove olderLinesView before creating linesView (same view) the linesView is not created at all. And if i don't remove this view before creating it, it redraws the view over and over. I am calling this code again and again so need to remove this view before creating its new instance. What i am doing wrong here?

You need to modify code as below
UIView *olderLinesView = (UIView*)[self.view viewWithTag:1820];
for (UIView* view in [tableview subviews])
{
if( [view isKindOfClass:[olderLinesView class]] )
{
for (UIView* subview in [view subviews])
{
[subview removeFromSuperview]; //if you want all views
}
}
}
///(OR)
for (UIView* view in [tableview subviews])
{
if( [view isKindOfClass:[olderLinesView class]] )
{
for (UIView* subview in [view subviews])
{
if(subview.tag==1820)
{
[subview removeFromSuperview];//if you want remove single view
}
}
}
Hope it helps you...!

Related

How to setup accessibility for all table cell contents in iOS?

I setup the accessibility for the table cell successfully. But I have two cases:
If I set the accessibility to self.containerview, I need to tap on each element and it gives the voice over for each element.
If I set the accessibility to self, the voice over is reading the cell content except the image view. Somehow, the voiceover is neglecting the image view and is only reading the uilabels.
This is how I setup the accessibility:
-(void)awakeFromNib{
[UIViewController makeViewAccessible:self]; //tried self.containerView, If i put self here, voice over is not reading the imageview
}
and the method:
+(void)makeViewAccessible:(UIView *)view {
for(UIView *subview in [view subviews]){
if([subview isMemberOfClass:[UIView class]]){
[self makeStaticViewAccessible:subview];
}else if([subview isMemberOfClass:[UILabel class]]){
[subview setIsAccessibilityElement:YES];
[subview setAccessibilityTraits:UIAccessibilityTraitStaticText];
} else if([subview isMemberOfClass:[UIImageView class]]){
[subview setIsAccessibilityElement:YES];
[subview setAccessibilityTraits:UIAccessibilityTraitImage];
}
}
}
You should disable accessibility of self and self.containerView.
Ex: [self.containerView setIsAccessibilityElement:NO]
Now you can set accessibility of each subviews, by using your custom method(+(void)makeStaticViewAccessible:(UIView *)view).

Disable interaction for views behind background image

To show a help view I've put an UIImageView which behind has some UIButtons. How can I disable user interaction of these buttons? If I touch this image where buttons are behind, they responds to touch events.
CODE FOR IMAGE BACKGROUND:
self.helpBackground = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
self.helpBackground.backgroundColor = [UIColor colorWithWhite:0 alpha:0.75];
self.helpBackground.hidden = YES;
[self.view addSubview:self.helpBackground];
I've used self.helpBackground.userInteractionEnabled = NO; but didn't work.
Thanks.
Put your buttons in a array and loop through them and disable them:
NSArray *buttonsArray = #[yourButton1, yourButton2];
for (UIButton *button in buttonsArray) {
button.enabled = NO;
}
And when you want them enabled just loop again and set enabled to YES
Keep a Tag to your helpView(imageview) and add the following code
for (UIView *view in self.view.subviews) {
if (view.tag != yourViewTag) {
view.userInteractionEnabled = NO;
}
}
And after removing the help screen use the following code
for (UIView *view in self.view.subviews) {
view.userInteractionEnabled = YES;
}
you can try below solution..When help imageview is appearing
for (UIView *view in [self.view subviews])
{
if (view isKindOfClass:[UIButton Class])
{
view.userInteractionEnabled = NO;
}
else
{
view.userInteractionEnabled = YES;
}
}
////After dismissing the help screen you can
for (UIView *view in [self.view subviews])
{
if (view isKindOfClass:[UIButton Class])
{
view.userInteractionEnabled = YES;
}
}
////(OR) Simply do as below
self.view.userInteractionEnabled=YES;
Hope it helps you..
You can create a method that disables user interaction for all views that are under your helpBackground:
- (void)disableUserInteractionForViewsUnderView:(UIView *)view
{
CGRect area = view.frame;
for (UIView *underView in [self.view subviews]) {
if(CGRectContainsRect(area, underView.frame))
underView.userInteractionEnabled = NO;
}
}
and after that you call it where you need it:
[self disableUserInteractionForViewsUnderView:self.helpBackground];
EDIT
I've created a UIViewController category gist on github.com. You can find it here: https://gist.github.com/alexcristea/0244b50e503e8bf4f25d
You can use it like this:
[self enumerateViewsPlacedUnderView:self.helpBackground usingBlock:^(UIView *view) {
if ([view isKindOfClass:[UIButton class]]) {
view.userInteractionEnabled = NO;
}
}];

Iphone remove sub view

I have a UINavigationController. On the right top i have a button on click of which i have to get a drop down table view. I created another UIViewController Class, with xib and added it as a subView to the current view. It should appear on 1st click and disappear on the 2nd click. This should happen for all click(open view and close view). I wrote this code but dont know where i'm going wrong. someone please help
-(void)modalTableView
{
tableView1 = [[TableViewController alloc] initWithNibName:#"TableViewController" bundle:nil];
for (UIView *subView in self.view.subviews)
{
if ([subView isKindOfClass:[TableViewController class]])
{
[subView removeFromSuperview];
}
else
{
[self.view addSubview:tableView1.view];
}
}
}
What am i missing here?
EDIT : TableViewController is the name of my UIViewController Class
The clue is here
for (UIView *subView in self.view.subviews)
each subView is of class UIView and your test
isKindOfClass:[TableViewController class]
is testing for class TableViewController
I would suggest a way of doing this would be by tagging the views that you add dynamically, with say 99 - and then in your loop you can identify those views by their tag.
eg.
for (UIView *subView in self.view.subviews)
{
if (subView.tag == 99)
{
[subView removeFromSuperview];
}
}
Swift version
To remove a single subview:
subView.removeFromSuperview()
To remove all subviews:
for subView in self.subviews as [UIView] {
subView.removeFromSuperview()
}
Source: What is the best way to remove all views from parent view / super view?
Try this,
if ([subView isKindOfClass:[UITableView class]])
{
[subView removeFromSuperview];
}
Here is something that should go some way to working - assuming that tableView1 is a retained #property (If not then maybe this SO answer on lazy loading techniques is for you).
-(void)modalTableView
{
if (tableView1 != nil)
{
tableView1 = [[TableViewController alloc] initWithNibName:#"TableViewController" bundle:nil];
}
if (tableView1.view.superview == nil)
{
[self.view addSubview:tableView1.view];
} else
{
[tableView1.view removeFormSuperview];
}
}

ios 5 uiwebview shadows

i'm trying to display pdf file in uiwebview. my pdf files have different size.
one can see the shadow or gradient outside of pdf in uiwebview.
i've tried different solution but with no luck. see the link.
how can i get rid of those shadows?
duplicate with this answer, but not working for me
the code accepted as aswer there:
webView.backgroundColor = [UIColor whiteColor];
for (UIView* subView in [webView subviews])
{
if ([subView isKindOfClass:[UIScrollView class]]) {
for (UIView* shadowView in [subView subviews])
{
if ([shadowView isKindOfClass:[UIImageView class]]) {
[shadowView setHidden:YES];
}
}
}
}
There a several threads around here with this topic …
Try this:
for(UIView *wview in [[[webView subviews] objectAtIndex:0] subviews]) {
if([wview isKindOfClass:[UIImageView class]]) { wview.hidden = YES; }
}
It should work and pass App Store submission, because it's not using any private APIs.

Remove gradient background from UIWebView?

How do remove the gradient from a UIWebView - the one that you see if you overscroll the top or bottom.
This code
webView.backgroundColor = [UIColor whiteColor];
just changes the color of the gradient, it doesn't removes it. How can this be done?
(note: not the same question as UIWebView underside)
Aha, yes terminology fail. I wouldn't call that a shadow at all, but c'est la vie. Here is my type-safe code to achieve the effect. To summarise: this will hide any image-view children of the scroll view. It's not as vulnerable to change as the (objectAtIndex:0) methods, so if Apple re-order the children of the webView control it will work fine, but still relies on the fact that the gradient effect is applied by imageviews parented to the scroll view (and that there is indeed a scrollview underpinning the web view).
{
webView.backgroundColor = [UIColor whiteColor];
for (UIView* subView in [webView subviews])
{
if ([subView isKindOfClass:[UIScrollView class]]) {
for (UIView* shadowView in [subView subviews])
{
if ([shadowView isKindOfClass:[UIImageView class]]) {
[shadowView setHidden:YES];
}
}
}
}
}
To transparent the UIWebView and remove the scrolls.
webView.opaque = NO;
webView.backgroundColor = [UIColor clearColor];
for(UIView *view in webView.subviews){
if ([view isKindOfClass:[UIImageView class]]) {
// to transparent
[view removeFromSuperview];
}
if ([view isKindOfClass:[UIScrollView class]]) {
UIScrollView *sView = (UIScrollView *)view;
for (UIView* shadowView in [sView subviews]){
//to remove shadow
if ([shadowView isKindOfClass:[UIImageView class]]) {
[shadowView setHidden:YES];
}
}
}
}
for hide scroll indicators
You mean the shadow? Remove UIWebView Shadow?
The only way I found how to do this was :
for(UIView *aView in [[[webView subviews] objectAtIndex:0] subviews]) {
if([aView isKindOfClass:[UIImageView class]]) { aView.hidden = YES; }
}
It just just steps thru the subviews of UIWebView and removes the view if it is an image view.
I haven't put this in any App Store apps, so I don't know if Apple would accept it.
EDIT: Brian's link provides more details.
Using method suggested above you won't be able to edit your scroll indicator/insets later. They appear as UIImageView also, so you should check for last object:
UIView* lastView = [[subView subviews] lastObject];
for (UIView* shadowView in [subView subviews])
{
if(shadowView!=lastView) ... <-this one is a scroll
}
I was able to do this by adding white subviews to the top and bottom of the WebView’s scrollView. I control the content of the WebView, so I know that white is OK - this won’t work if you are loading arbitrary content.
// _topCover and _bottomCover are ivar UIViews
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
// with cover views 300pt high, I couldn't scroll far enough to see the shadow,
// even in portrait on an iPad, which gives you the longest scroll distance
CGFloat coverage = 300;
_topCover = [[UIView alloc] initWithFrame:CGRectMake(0, -coverage, webView.bounds.size.width, coverage)];
_bottomCover = [[UIView alloc] initWithFrame:CGRectMake(0, webView.scrollView.contentSize.height, webView.bounds.size.width, coverage)];
_topCover.backgroundColor = [UIColor whiteColor];
_bottomCover.backgroundColor = [UIColor whiteColor];
// in case the webView is resized, e.g. by rotating the device
_topCover.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth;
_bottomCover.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
[webView.scrollView addSubview:_topCover];
[webView.scrollView addSubview:_bottomCover];
}
I run it it after the page loads so that webView.scrollView.contentSize.height will give me the correct height. I’m not sure how this will work if your pages are dynamically changing height. My page loads only once; if yours is reloading, you will want to skip running alloc/init on _topCover and _bottomCover after the first time for efficiency.
Update: I’m not sure that my use of autoresizingMask, above, is sufficient when the view rotates. You may need to put this in the UIViewController that contains your UIWebView to resize the covers after rotating:
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
CGFloat coverage = 300;
_topCover.frame = CGRectMake(0, -coverage, self.webView.bounds.size.width, coverage);
_bottomCover.frame = CGRectMake(0, self.webView.scrollView.contentSize.height, self.webView.bounds.size.width, coverage);
}
I've built upon #damithH 's answer
#implementation UIWebView (Extensions)
- (void)setBackgroundAndShadowVisible:(BOOL)visible
{
self.opaque = !visible;
self.backgroundColor = [self.backgroundColor colorWithAlphaComponent:visible ? 1.0 : 0.0];
for(UIView *view in [self subviews])
{
if([view isKindOfClass:[UIImageView class]])
{
view.hidden = !visible;
}
if([view isKindOfClass:[UIScrollView class]])
{
UIScrollView *scrollView = (UIScrollView *)view;
for (UIView *shadowView in [scrollView subviews])
{
if ([shadowView isKindOfClass:[UIImageView class]])
{
shadowView.hidden = !visible;
}
}
}
}
}
#end
if (UIDevice.currentDevice.systemVersion.intValue < 7)
for (UIImageView *imageView in webView.scrollView.subviews)
if ([imageView isKindOfClass:[UIImageView class]] && imageView.image.size.width == 1)
imageView.hidden = YES;

Resources