【问题标题】:how I can know contentOffset of UITextField?我怎么知道 UITextField 的 contentOffset?
【发布时间】:2013-10-21 17:17:21
【问题描述】:

我有一个高度为 500 的 UIScrollView,UITextFileds 位于位置 0、100、200、300、400、500(1 个 UITextFileds、UITextFileds 2、3 个 UITextFileds、UITextFileds aUITextFileds 4 和 5)。

ContentOffset 位于 (0, 70) 位置。我点击UITextField 4,怎么知道TextField 4 contentOffset的位置??

更新:

我点击了 UITextField 4,我怎么知道我必须向上 Y 像素,以便 UITextField 不会被键盘隐藏?

【问题讨论】:

  • 这是文本字段的框架原点。您正在根据内容大小(而不是可见边界)工作。如果您想...
  • 好的!我的问题是,当她在 UIScrollView 中间不保持隐藏时,我看不到如何提升 UIScrollView Y 像素。 .如果 UITextField 与底部齐平,我会升高和降低到 UIScrollView @Wain
  • 您是否尝试过始终将选定的文本视图滚动到顶部?

标签: objective-c uiscrollview uitextfield uikeyboard contentoffset


【解决方案1】:

我的代码:

 - (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.


}

-(void) viewWillAppear: (BOOL) animated {

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:)  name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasHidden:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture
{

}


- (void)setScrollView:(UIScrollView *)scrollView offsetToView:(UIView *)view
{

    CGRect contentFrame = CGRectMake(view.frame.origin.x, view.frame.origin.y,view.frame.size.width, view.frame.size.height);
    CGPoint contentOffset = CGPointMake(0, contentFrame.origin.y);

    scrollView.contentOffset = contentOffset;

}


- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    [self setScrollView:_scrolviewKeys offsetToView:textField];
}

- (void)keyboardWasShown:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    _scrolviewKeys.frame = (CGRect){
        _scrolviewKeys.frame.origin,(CGSize){
                _scrolviewKeys.frame.size.width,_scrolviewKeys.frame.size.height-kbSize.height}


    };
}

点击 UITextView 黄色

结果:

【讨论】:

  • 好的,我更新了我的答案,但这个网站不是要为你询问其他编码,你应该尝试理解和提高你的编码技能;)
  • contentOffset.height = textField.origin.y + scrollView.size.height - textField.size.height;
  • scrollView.contentOffset.y 或 scrollView.frame.size.height,而不是 contentOffset.height @Francescu
  • 我一个月前开始使用iOS,已经很难理解@Francescu的东西
【解决方案2】:

应该很简单:

- (void)setScrollView:(UIScrollView *)scrollView bottomOffsetToView:(UIView *)view
{
   scrollView.contentOffset = CGPointMake(0, view.frame.origin.y + view.frame.size.height - 216);
}

然后在您的代码中将自己设置为 UITextFieldDelegate 并实现:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
     self.currentTextField = textField;
}

更新:

你在这里问了两个不同的问题。对于键盘通知的键盘注册(例如viewWillAppear):

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:)  name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasHidden:) name:UIKeyboardWillHideNotification object:nil];

然后根据键盘大小改变滚动视图框架:

- (void)keyboardWasShown:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    self.scrollView.frame = (CGRect){self.scrollView.frame.origin,(CGSize){self.scrollView.frame.size.width,self.scrollView.frame.size.height-kbSize.height);
    if (self.currentTextField != nil)
    {
        [self setScrollView:self.scrollView bottomOffsetToView:self.currentTextField];
    }
}

keyboardWasHidden 的行为相同(重置初始帧),不要忘记删除 viewDidDisappear 中的 Observer。

【讨论】:

  • 好的!我的问题是,当她在 UIScrollView 中间不保持隐藏时,我看不到如何提升 UIScrollView Y 像素。 .如果 UITextField 与底部齐平,我将向上和向下移动到 UIScrollView。 @弗朗西斯库
  • 键盘问题完全不同..只需注册通知并更改您的滚动视图内容偏移或插入..
  • @Francescu 行中的错误:scrollView.contentOffset = CGSizeMake(0,view.frame.origin.y+scrollView.frame.size.height-view.frame.size.height); 错误:从不兼容的类型“CGSize”(又名“struct CGSize”)Assingin 到“CGPoint”(又名“Struct CGPoint”)
  • 在第一个函数中将 CGSizeMake 替换为 CGPointMake。如果它不起作用,我会删除我的答案。
  • 将 scrollView.frame.size.height 替换为 KeyboardHeight 并且可以正常工作。 @弗朗西斯库
【解决方案3】:

在 iOS 7 中,scrollView 将自动滚动最小量以保持 textField 可见。如果您使用的是 iOS 6,您可以执行以下操作来模拟相同的行为。

假设您在键盘如下所示后调整了scrollView.contentInset,这是我在- (void)textFieldDidBeginEditing:(UITextField *)textField中所做的:

self.scrollView.contentInset = (UIEdgeInsets){.bottom = 216.0f};
self.scrollView.scrollIndicatorInsets = self.scrollView.contentInset;

CGFloat minY = CGRectGetMinY([self.view convertRect:textField.frame fromView:textField]);
if (minY > self.scrollView.contentInset.bottom) {
    CGFloat visibleHeight = CGRectGetHeight(self.scrollView.bounds) - self.scrollView.contentInset.bottom;
    [self.scrollView setContentOffset:CGPointMake(0.0f, CGRectGetMaxY([self.view convertRect:textField.frame fromView:textField]) - visibleHeight) animated:YES];
}

我们首先根据scrollView的frame计算textField的frame的minY,然后看看它是否大于我们插入scrollView的量。如果是,那意味着 scrollView 的这一部分现在位于键盘下方。然后我们通过考虑visibleHeight 来计算偏移量,这是显示键盘后scrollView 可见的内容。获取 textField 框架的 maxY 并减去 visibleHeight 以使其与键盘顶部对齐,从而将 scrollView 移动到使其可见的最小量。

【讨论】:

    猜你喜欢
    • 2015-12-31
    • 2017-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多