iOS XCode Add the scrollbar indicator program programmatically - ios

I'm currently coding a iOS apps that contains a scrollview in the mainview. When the user want to reset the scrollview I'm running this method:
-(void)clearFlightTimer{
// Removing object (button, label and image) from subview
// *** KNOWN BUG WHEN REMOVING UIImageView OBJECT IT'S ALSO REMOVING THE SCROLLBAR IMAGE ***
for(UIView *subview in [self.flightViewScrollView subviews]) {
if([subview isKindOfClass:[UIButton class]] || [subview isKindOfClass:[UILabel class]] || [subview isKindOfClass:[UIImageView class]]) {
[subview removeFromSuperview];
} else {
// Do nothing - not a UIButton or subclass instance
}
}
// Reloading the View
[self viewDidLoad];
[self viewWillAppear:(YES)];
}
The issue is that the scrollbar seem to be part of UIImageView class and this method remove it. How could I keep the scrollbar image when removing all object from this subview? Is there a way to code the scrollbar indicator back programmatically?
Thanks!
Hope that I'm clear!

The simplest and most fool proof way is probably to add an intermediate subview of the scroll view that contains your actual content. This new container view's subviews can be iterated over and removed as you need. Alternatively, if you know the scroll view is in an empty state, you might be able to iterate over subviews of the scrollview and store all image views that exist when empty - those views will be the ones you shouldn't mess with.

Related

removeFromSuperview UIImageView not working

I am trying to remove an image from an existing view using the code below:
-(void) deleteImage:(int)imageID{
//[self.imageArray removeObjectAtIndex:imageID];
//remove the image from the screen
for (UIView* view in self.view.subviews) {
if ([view isKindOfClass:[UIImageView class]] && [view tag] == imageID) {
//could be a bug here with the re-Ordering of the array (could add a helper method to reset all the tags on screen when this is called
NSLog(#"view %#", view);
//[self.imageArray removeObjectAtIndex:imageID];
[view removeFromSuperview];
}
}
}
The NSLog outputs the following:
view <UIImageView: 0x18b8d7b0; frame = (0 0; 768 2016); autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x18b8d890>>
The issue i have is that it appears to be getting the ImageView but it doesn't remove it.
Since you're trying to modify the user interface, you must be doing it on the Main thread. Therefore, do the following:
dispatch_async(dispatch_get_main_queue(), ^{
[view removeFromSuperview];
});
First of all, when you ask a view for its subviews, you get a copy of the subviews array, so fiddling with the copy array doesn't do you any good.
Secondly, in principle, you should be getting a crash when you try to remove the subview, because you are using fast enumeration, which prohibits mutating the collection while in the enumeration.
Lastly, the correct method of removing a subview of a view is to use the removeFromSuperview method, which implies that you do need to keep a reference to the image view in question.
Essentially, you should use the methods provided in the UIView section titled 'Managing the View Hierarchy', and not to be grubbing around with the actual subview arrays themselves. That's a recipe for potentially getting the subviews hierarchy into an inconsistent state.

Synchronized hierarchy of UIViews and CALayers

I have the following hierarchy:
view
scrollview(with scroll and zoom enabled)
containerview
imageview (with a map)
views for buttons
views for navigation panel
Over the image view I have to put some point of interest (depending on the category the user selects):
The problems are:
if i attach them to the container view, they getting the correct position is very easy,
but when zooming they will zoom with the image, and they should keep the same size (1)
if i attach them to the scrollview I don’t see nothing..
If I attach them to the main view, I have two problems:
1 to find their hierarchy position in between scrollview and view for buttons, etc (they get at the top of the application and should be just over the map but below the control panels, buttons, etc
2 to coordinate with the zoom and scroll- done with convertPoint:toView
If I add a view (sibling of container view under the scrollview) they show fine, but I have no user input on them (They don’t receive
touches)
If I convert that sibling subview to a CALayer, I don’t get the sublayers (POIs) to shown.
(1) Ive tried to subclass container view and override setTransform by applying CGAffineTransform invertedTransform = CGAffineTransformInvert(transform); but I had problems passing it an array with views references to it
What would you recommend to do in this case?
Thank you very much!
S.
I've finally added subviews to containerView and I've subclassed it:
- (void)setTransform:(CGAffineTransform)transform
{
[super setTransform:transform];
CGAffineTransform invertedTransform = CGAffineTransformInvert(transform);
for (UIView *view in self.subviews)
{
if ([view isKindOfClass:[MarkerClass class]]) {
[view setTransform:invertedTransform];
}
}
}
The only problem is that before I zoom the containerView, a short sized version of the markers appear on the screen.
After passing some time, I've found I would override addSubview too, to get an initial transform in this way:
-(void)addSubview:(UIView *)view {
[super addSubview:view];
if ([view isKindOfClass:[MarkerClass class]]) {
CGAffineTransform invertedTransform = CGAffineTransformInvert(self.transform);
[view setTransform:invertedTransform];
}
}

Looping Through UIScrollView and Setting UImageView

I am trying to implement lazy loading of images in a UIScrollView but I am having some problems trying to set the images.
The user scrolls and I use the following to detect what the next view tag should be. I then want to be able to loop through the corresponding UIView and set its UIImageView image.
The following is not working and I was hoping someone could help me to correct it.
for (UIView *subview in self._scrolViewForEffects.subviews){
for (UIImageView *view in subview.subviews){
if([view isKindOfClass:[UIImageView class]]){
if(view.tag == currentPage){
if(view.image == nil){
[view setImage:[UIImage imageNamed:#"Theme1.png"]];
}
}
}
}
}
It is not enough information to say what problem you can have but I suggest you to execute in debugger window a command
po [self._scrolViewForEffects recursiveDescription]
to check your view hierarchy at runtime.
Turns out that it wasn't to do with the code above. It turns out that I wasn't setting the tag on the UIImageView when I was creating it.
Thanks for the help!

Adding UISearchBar with Scope button as subview to UIView

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;
}
}
}
}

Maps and legal mention

With iOS < 6.0 we were able to re-position the "Google" link over map view (by browsing the subviews of map view). Now with iO6, there's a "legal" link and this is a MKAttributeLabel. A private class that we can't manipulate ...
My problem is that I must add a footer subview to my map and it'll hide the legal link ...
How can I solve this problem without any App Store rejection ?
Can I create another legal button my self and add it where I want in my map view ?
I have no idea what I'm able to do...
There's a few answers recommending you move the legal label in the viewDidAppear of your view controller, however this doesn't work if you then resize your map view (like I am).
The best way is to subclass the MKMapView and override the layoutSubviews method. In my example I just needed to nudge the legal label above a semi-transparent toolbar.
-(void)layoutSubviews
{
[super layoutSubviews];
UILabel *legalLabel;
for(UIView *view in self.subviews)
{
if([view isKindOfClass:[UILabel class]])
{
legalLabel = (UILabel *)view;
break;
}
}
legalLabel.center = CGPointMake(legalLabel.center.x, self.bounds.size.height - 55.0f);
}
Does the footer view have to be inside the map boundary, why not put the map and the footer into the same super view?

Resources