I am developing iOS App.
I add UITextView(100px height) on top of UIWebView and would like to make them scroll simultaneously.
I am writing down the following code. However, when I scroll my screen, only UIWebView is scrolled and UITextView isn't.(UITextView is fixed!)
I hope that when I scroll up my screen, both UIWebView and UITextView go up, and vice versa.
Could you tell me how to solve?
#interface DetailViewController ()<UIWebViewDelegate>{
UITextView *recommendText;
}
#property (weak, nonatomic) IBOutlet UIWebView *detailPage;
- (void)viewWillAppear:(BOOL)animated{
[self headerMake];
}
-(void)headerMake{
recommendText = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 320,100)];
recommendText.layer.borderWidth = 2;
recommendText.text = #"hogehoge";
[self.detailPage addSubview:recommendText];
[[self.detailPage scrollView] setContentInset:UIEdgeInsetsMake(100, 0, 0, 0)];
}
A text view is a scroll view subclass, and web views have a scrollView property.
Listen to both scroll views content offset change (using scrollViewDidScroll:) and update the content offset of the other scroll view accordingly. This will give you the effect of scrolling the two at the same time.
self.textview.delegate = self;
self.webview.scrollView.delegate = self;
...
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if(scrollView == self.textview)
{
self.webview.scrollView.contentOffset = self.textview.contentOffset;
}
else
{
self.textview.contentOffset = self.webview.scrollView.contentOffset;
}
}
If i understand your question correctly you should embed them both is a scroll view and disable scrolling in the UIWebview and the UITextView. then they will scroll as one. Although you may have to dynamically set the sizes of the Textview and webview if they have different content they have to support.
Related
I have two table views that I use as input accessory views in my project. One is created programatically, and displays perfectly well. The second is a custom table view with custom cells made in storyboard and stored as a property of the view controller as a strong reference.
#property UITableView *firstTable; #property (strong, nonatomic) IBOutlet UITableView *secondTable;
They are both initiated in viewDidLoad as follows:
self.firstTable = [[UITableView alloc] initWithFrame:
CGRectMake(0, 0, self.view.bounds.size.width, 100) style:UITableViewStylePlain];
self.firstTable.delegate = self;
self.firstTable.dataSource = self;
self.firstTable.scrollEnabled = YES;
self.firstTable.rowHeight = 30;
self.firstTable.backgroundColor = [[SwagCommonFunctions alloc] colorWithHexString:#"f4ecea"];
self.secondTable.frame = CGRectMake(0, 0, self.view.bounds.size.width, 100);
They are set as input accessory views in another function
[textView setInputAccessoryView:self.firstTableView];
[textView reloadInputViews];
OR
[textView setInputAccessoryView:self.secondTableView];
[textView reloadInputViews];
firstTable works perfectly. secondTable is never loaded because frame height is zero at the time of cellForRowAtIndexPath. If the frame height is manually set at 100 (the width is full screen) with origin of (0,0) in numberOfRowsInSection, secondTable appears as expected with the correct data, but the frame obscures the entire keyboard instead of resting in the normal inputAccessoryView location.
How do I set the frame of secondTable to behave correctly?
self.secondTable.autoresizingMask = UIViewAutoresizingNone;
I have a .xib file who have one view, one tab bar and other 3 scrollViews, when the user select a new tab bar item I execute this code:
//Views e Scrolls
IBOutlet UIView *myView;
IBOutlet UIScrollView *myScroll;
IBOutlet UIScrollView *myScroll2;
IBOutlet UIScrollView *myScroll3;
#property (nonatomic) UIScrollView *scroll;
-(void)tabBar:(UITabBar *)tabBar
didSelectItem:(UITabBarItem *)item{
NSArray *viewsToRemove = [myView subviews];
for (UIView *v in viewsToRemove) {
[v removeFromSuperview];
}
if(item.tag == 1){
self.title = #"scroll 1";
scroll = myScroll;
}
if(item.tag == 2){
self.title = #"scroll 2";
scroll = myScroll2;
}
if(item.tag == 3){
self.title = #"scroll 3";
scroll = myScroll3;
}
scroll.contentSize = scroll.frame.size;
scroll.frame = myView.frame;
scroll.scrollEnabled = YES;
[myView addSubview:scroll];
[myView setBackgroundColor:[UIColor clearColor]];
}
This code works great, but when I select one of the scrolls that had previously been removed from view, they lose their scrolling (which had not happened before), why this is happening and how to solve?
It's this line here:
scroll.contentSize = scroll.frame.size;
When your UIScrollView's contentSize matches its frame.size, it doesn't need to scroll (by definition). You need to figure out the actual size of its content and use that. You can at least demonstrate the correct functionality (even if it isn't with correct values) by doing this:
scroll.contentSize = CGSizeMake(scroll.frame.size.width*2, scroll.frame.size.height*2);
You'll have to find the real content size for yourself, though.
Assuming I understand your intention correct: Removing a scollview from it superview will also remove it from the responderchain. It cant no longer respond to touch event - thus wont scroll.
The question itself and your purpose of code is not very clear. You may try to explain further. And check this line -
scroll.contentSize = scroll.frame.size;
A UIScrollView whose contentSize <= frame.size, will not scroll. To make it scroll, contentSize should be bigger than frame.size
You should usually set the contentSize depending on the amount of content (subviews) for the scrollView.
In the next line you changed the frame.size, but I do not think that makes the frame size smaller than contentSize in your case.
I am developing a media based application for iOS, It has a scrollview which has three subviews in it - a UIImageView, a title UILabel and a UIButton.
When I scroll the scrollview I want the UILabel to be fixed (i.e it shouldn't scroll with the scrollview) untill the UIImageView scrolls up completely.
Please can you help me, how can I keep the title label frame at a constant height until the scroll view scrolls to a certain height.
I would first make sure the UILabel isn't a subview of the scroll view. Then add <UIScrollViewDelegate> to your view controller class. Then in your .m file add
- (void) scrollViewDidScroll: (UIScrollView *) scrollView
{
CGFloat y = scrollView.contentOffset.y;
if (y > (your number here)) {
// Do something with the UILabel
}
}
Already answered here
I have a xib with a View, I changed the View class to be UIScrollView.
Now I have set contentSize to some big number (2000, 2000).
Here is my code:
This allows talking to my scrollview without casting every time:
- (UIScrollView *)scrollView {
return (UIScrollView *)self.view;
}
This I do in viewDidLoad:
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, 2600);
self.scrollView.scrollEnabled = YES;
I know viewDidLoad is getting called fine. But the scrollView is not reacting. I don't have any touch responders or anything that can absorb touches.
For this, first set delegate of scrollView inside your xib files then create an IBIoutlet for your scrollview. Remind that scrollView should connect with its refrencing outlet then Assign a propertyy as Strong to your scrollView.
#property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
and synthesize it.
#synthesize scrollView;
then try to set contentSize.
scrollView.delegate=self;
scrollView.contentSize = CGSizeMake(self.view.frame.size.width, 2600);
scrollView.scrollEnabled = YES;
I hope it works for you.
I have found a problem, I was setting superview too small.
I want to add a view to the bottom of the content view of both a collection view and table view (and hence is applicable to any kind of scroll view) and I also want to be able to scroll down to see this view e.g.:
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
// Observe change in content size so can move my view when
// content size changes (keep it at the bottom)
[self addObserver:self forKeyPath:#"contentSize"
options:(NSKeyValueObservingOptionPrior)
context:nil];
CGRect frame = CGRectMake(0, 0, 200, 30);
self.loadingView = [[UIView alloc] initWithFrame:frame];
[self.loadingView setBackgroundColor:[UIColor blackColor]];
[self addSubview:self.loadingView];
// Increase height of content view so that can scroll to my view.
self.contentSize = CGSizeMake(self.contentSize.width, self.contentSize.height+30);
}
return self;
}
However when, for example, a cell is inserted the contentSize is recalculated and whilst my view is still visible at the bottom of the content size (due to being able to bounce the scroll view) I can no longer scroll to it.
How do I ensure that the content size stays, as in my code, 30 points taller?
An additional question is:
is there any other way to track content size other than observing it?
Thanks in advance.
I have tried:
- (void)layoutSubviews
{
[super layoutSubviews];
self.contentSize = CGSizeMake(self.contentSize.width, self.contentSize.height+30);
}
However this causes all sorts of display issues.
If i understand correctly, you want to show a loading view in the tableView (f.e.) at the bottom. You could add an extra UITableViewCell containing this LoaderView to the tableView.
(Must change the numberOfRowsInTableView)
In another perspective for scrollViews: Use smaller bounds then the content itself, to make it scrollable. For example frame = fullscreen. At every cell adding or modification in subviews (adding) contentSize = content size + 30 px.
Try making a subclass of the scroll view and override the contentSize getter to return always 30 px more.
- (CGSize)contentSize {
CGSize customContentSize = super.contentSize;
customContentSize.height += 30;
return customContentSize;
}
(I'm writing the code by memory, there may be errors)