I would like to know how to access a UIScrollView using a subview UILabel.
I have tried to access the UIScrollView using .superview; however I am now receiving an error
No visible #interface for 'UIView' declares the selector 'scrollRectToVisible:animated:'
The code I am using looks like this
- (void) SymbolButtonPressed:(NSString *)selectedString {
UILabel *label = (UILabel *)[self.view viewWithTag:currentlySelectedTag];
// perform scrolling here, figure out what view your uilable is in.
float newPosition = label.superview.contentOffset.x+label.frame.size.width;
CGRect toVisible = CGRectMake(newPosition, 0, label.superview.frame.size.width, label.superview.frame.size.height);
[label.superview scrollRectToVisible:toVisible animated:YES];
}
The superview of a UILabel is of type UIView and so does not respond to the method you are trying to call. You can cast the superview as a UIScrollView so that Xcode can see the methods and properties you are trying to access. You should also check if the superview responds to the method.
if([label.superview respondsToSelector:#selector(scrollRectToVisible:animated:)]) {
[(UIScrollView *)label.superview scrollRectToVisible:toVisible animated:YES];
}
Given your sample code you will also need to cast the superview to get contentOffset
float newPosition = ((UIScrollView *)label.superview).contentOffset.x+label.frame.size.width;
Related
The Swipe delete button shows depends on TableviewCell Height.
Need to reduce the height of the delete button.
Can anyone help me please?
It is not a good practice to change native controls, but you still can do it by subclassing UITableViewCell
#implementation UITableViewCellSubclass
- (void)layoutSubviews {
[super layoutSubviews];
if (self.showingDeleteConfirmation) {
if ([self.subviews count] < 4) return;
UIView *deleteButton = [self.subviews objectAtIndex:3];
deleteButton.frame = CGRectOffset(deleteButton.frame, 10, 10);
}
}
#end
But, it is very bad way to handle it. Better create custom UITableViewCell with custom behaviour and custom delete UIButton and then do whatever you want with it.
I have a UITextField that I want to create a custom class on. So I created a file with a subclass of UITextField. Next, in the custom class, I want to implement a tableView. Kind of like a auto-complete textField.
I started creating it, and added the tableView like this:
[self addSubview:self.tableView];
When I run the app, the tableView is in the textField, so I can only see part of the tableView. How can I add it as a subview so I can see the full tableView?
This is what you are looking for
https://github.com/gaurvw/MPGTextField
This uitextfield subclass does what you want - it's builed for 'search' feature.
If you still want to use your own,
add tableview not to uitextfield itself, but like
[[self superview] addSubview:tableViewController.tableView];
EDIT:
you can set frame as:
CGRect frameForPresentation = [self frame];
frameForPresentation.origin.y += self.frame.size.height;
frameForPresentation.size.height = 200;
[tableViewController.tableView setFrame:frameForPresentation];
The way to add subview to uitextfield is to overload layoutSubviews method and init your tableview there:
- (void)layoutSubviews
{
[super layoutSubviews];
if (!self.tableview.superview)
{
[self setupView];
}
}
This will add the tableView as the subView of the textField.
self.tableView.frame = CGRectMake(0, CGRectGetHeight(self.bounds), CGRectGetWidth(self.bounds), YOUR_TABLE_HEIGHT);
[self addSubview:self.tableView];
self.clipsToBounds = NO;
However, a better way is to make the tableView as the textField's superView's subView, that is, the textField and the tableView should be siblings.
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.
Update: I ended up implementing the code below into it's own method and then called it from viewDidLayoutSubviews and willAnimateRotationToInterfaceOrientation. Doing it from viewDidAppear (as suggested) would not resize the view when returning from a segue.
I have a UIView defined in a storyboard which I'm using for a header view on my UIViewController. I have a constant in my code for all header views to be 80 units high. I have a tag on the storyboard header view of 200. I thought I could use this tag to get the view, modify the height of the underlying CGRect, and then re-set the header view to the modified CGRect. That doesn't seem to affect the height however. What am I missing?
- (void)viewDidLoad
{
UIView *header = [self.view viewWithTag:200];
CGRect hrect = [header frame];
hrect.size.height = HEADER_HEIGHT;
[header setFrame:hrect];
...
Try to do that on viewDidAppear then call
[self setNeedsLayout]
The problem is that you're using the tag as an NSString. The tag property is an NSInteger. Try doing the following:
- (void)viewDidLoad
{
UIView *header = [self.view viewWithTag:200];// give the tag as an int
CGRect hrect = [header frame];
hrect.size.height = HEADER_HEIGHT;
[header setFrame:hrect];
...
Also, make sure the tag you defined in the Storyboard is also 200 and not #"200".
Hope this helps!
I have a UIScrollView that has UIViews in it, and in those UIViews are UIImageViews. I am trying to access the UIImageViews.
For proof of concept, I have a scrollview with a UIView, which contains a UIImageView in it, both whose tags are set to 0.
Code:
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
UIView *currentView = (UIView *)[scrollView viewWithTag:0];
UIImageView *currentImageView = (UIImageView *)[currentView viewWithTag:0];
[self bobbleView:currentImageView];
}
And then I am trying to bob that UIImageView up and down continuously (which is another matter in of itself, I can only get it to bobble once so I just that left that code in), but what is happening is the entire UIScrollView and its subviews are bobbing.
That code:
-(void)bobbleView:(UIView *)viewIn{
viewIn.transform = CGAffineTransformMakeScale(1.2, 1.2);
[UIView animateWithDuration:1 animations:^{
viewIn.transform = CGAffineTransformMakeScale(1.0, 1.0);
}];
}
Any thoughts?
The viewWithTag doc says:
Discussion
This method searches the _current view_ and all of its subviews for the specified view.
(emphasis mine)
What you are seeing is that:
(UIImageView *)[currentView viewWithTag:0]
returns
currentView
HTH
Yes.. Assign unique tags to the views and different to eachother. I bet that if you set a breakpoint in the call to bobbleView, your UIImageView object will be your UiScrollView and not the imageview nor the UiView... Use simply unique tags, like 100X fir the UIViews and 200X for the imageview