programigaclly使用子视图元素滚动UIScrollView

时间:2014-01-19 22:34:33

标签: ios uiscrollview uilabel superview

我想知道如何使用子视图UILabel访问UIScrollView。

我尝试使用.superview访问UIScrollView但是我现在正在收到错误

No visible @interface for 'UIView' declares the selector 'scrollRectToVisible:animated:'

我使用的代码看起来像这样

- (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];

}

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

UILabel的超级视图属于UIView类型,因此不会响应您尝试调用的方法。您可以将超级视图转换为UIScrollView,以便Xcode可以查看您尝试访问的方法和属性。您还应该检查superview是否响应该方法。

if([label.superview respondsToSelector:@selector(scrollRectToVisible:animated:)]) {
    [(UIScrollView *)label.superview scrollRectToVisible:toVisible animated:YES];
}

根据您的示例代码,您还需要转换superview以获取contentOffset

float newPosition = ((UIScrollView *)label.superview).contentOffset.x+label.frame.size.width;