How to get index of multiple scrollview in iOS - ios

i have horizontal and vertical scrollview, for example one scrollview have four button and and second scrollview have six button and more so, when i click on any button then got index of last scrollview view, i already using tag.
my problem is for example i click on third scroll then i want both tag
third scroll tag and specific button tag.

first tag of scrollview in storyboard .
set tag to button dynamically.(while create button or add button to scrollview)
track your index as below :
- (IBAction)clickonbutton:(UIButton *)sender {
UIScrollView* yourscrollview = (UIScrollView*)[sender superview]; // track your scrollview as per your view hierarchy
NSLog(#"%ld",(long)yourscrollview.tag);
NSLog(#"clicked button tag%#",sender.tag);
}

Related

ios-Update multiple UIViews on UIScrollview

I have a ViewController that contain a UIScrollView, that contains a few views which are custom UIViews (very simple uiimage + uibutton) that the user can scroll between (one custom view at the a time).
I want the user to be able to "mark" a photo, and then to display a certain text, when the user select a different photo (by using the button), I want to update the text on the previous selected photo and the current selected.
What should I do?
Inside the view itself I don't have access to the previous view, I figured I should send a notification to the view controller but than I have no access to the button I want to update
You could use tags to identify the scrollViews subviews.
Add a tag to the subview and the same tag to the button that belongs to the subview while adding them to the scrollView.
view.tag = 1;
button.tag = 1;
Then in the method called by the buttons, you can use the buttons tag to get the according custom view.
-(void)buttonClick:(id)sender{
UIButton *btn = (UIButton*)sender;
NSInteger tag = btn.tag;
UIView* customView = [self.scrollView viewWithTag:tag];
}
To access the prevoius selected view, store the tag of that view when editing for the next call of the function.

Scrolling a UIScrollView regardless of where the touch occurs

The end goal is to create the same scrolling effect as the one on the "Me" tab of the twitter iOS app where the segmented control rises to the top as you scroll down and then stays fixed to it unless scrolled back up to the top.
The solution I've come up with is illustrated below. There is a view at the top, a segmented control, and a table view beneath the segmented control. All of these elements are embedded inside a scrollview that takes up the entire screen (minus tab & nav bars).
Here is the key issue: If begin scrolling by swiping up from the top-most view or the segmented control, it scrolls the scrollview that all the elements are embedded in. If I scroll the tableview, it will only scroll itself and leave the top-most view and segmented control unaffected.
How can I scroll the scrollview that the elements are embedded in no matter where the scrolling occurs on screen?
I had a similar layout in one of my projects. I used SJSegmentedViewController.
It Requires a headerViewController ,datasource for middle segment and viewController array for those segments.
This library allows you to scroll from anywhere on the screen moreover the segmented control sticks to the top as user scrolls all the way to top.
Here is how you can implement this :
First import the module into your class
import SJSegmentedScrollView
Then create a headerViewController and two viewControllers(Say Video and Tips) for segment
let headerViewController = HeaderViewController()
let video = VideoController()
let tips = TipsController()
After that set these Controller and also set the title for segmented control as following:
segmentController.headerViewController = header
segmentController.segmentControllers = [video,tips]
video.title = "Video"
tips.title = "Tips"
Then add it to the Container View
addChildViewController(segmentController)
containerView.addSubview(segmentController.view)
segmentController.view.frame = self.containerView.bounds
segmentController.didMove(toParentViewController: self)
Here Container View is a UIContainerView
Last but make sure to call in child controllers (VideoController,TipsController), After calling this function in those controllers you can scroll from anywhere on the screen.
extension HomeListingViewController: SJSegmentedViewControllerViewSource {
func viewForSegmentControllerToObserveContentOffsetChange() -> UIView {
//Scrollview in child controllers
return scrollview
}
}
You can find the full documentation here
Hope this helps!

Overlap iOS components viewController

I have a UITableView in my View Controller and I would like to place two buttons (one on the left and other on the right side of the UITableView), overlapping this component.
How would I do this? If I place a button in one of the sides, the UITableView shrinks to fit the new button, but I'd like to view it over the table.
Create the button as a subview of the view controller's view (this is easiest to do in code, because interface builder would automatically add the button as a subview of the UITableView
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(40,40,80,80)]; //Whatever your need and repeat for your second button
[self.view addSubview:button];

Hiding a delete button when you tap outside the view

I have a delete button that shows up when I swipe left on a table view's row.
I want to be able to hide this button if the user taps anywhere else in the view. How do I do that? I tried putting a giant button on bottom of all views but the tap outside is not being detected by the button.
In the viewDidLoad, I added the view controller as a target:
[self.backgroundButton addTarget:self action:#selector(backgroundButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
And in the callback I just have a message:
- (IBAction)backgroundButtonTapped:(id)sender {
NSLog(#"BACKGROUND VIEW TOUCHED");
}
But when I tap outside in a general area, I do not see the message.
I resolved the issue as follows:
. Created a view TopView subclassed from UIView
. Changed class of my top level view to this class.
. Within my logic at the proper place, stored the subview that needs to be hidden in the top view
self.view.mySubview = subview;
. Overrode hitTest:withEvent in top view. Here I detect the tap outside that of the subview and hide the subview as required.

Bringing a subview in a UITableViewCell to front

I added a bunch of UILabel views to a UITableViewCell's contentView, some of which may overlap. On tapping any of the labels, I want to trigger some actions. Also, I want to bring up the tapped label to the top.
Using a UITapGestureRecognizer on the labels, I can figure out which one is tapped and perform the actions. But bringing the tapped and overlapped label to the front does not work. This is what I am trying:
UILabel *foundLabel = ....; // find the label
for (UITableViewCell *acell in theTable.visibleCells) {
UIView *cellContentView = acell.contentView;
if ([cellContentView.subviews containsObject:foundLabel]) {
[cellContentView bringSubviewToFront:foundLabel];
NSLog(#"should bring to front...");
}
}
I do get the NSLog output above, so I know that the bringSubviewToFront is being called on the appropriate cell's contentView. But no change in the subview layout order.
Ideas?
One thing to explore is zposition on the uitablviewcell's layer.
This successfully put on tableviewcell in front of another for me:
cell1.layer.zPosition=3 ;//above
cell2.layer.zPosition=2 ;//below
bringSubviewToFront: didn't work for me either
Try insertSubview:belowSubview: where the latter subview is a super view such as tab bar, nav bar, table view.

Resources