UIKit: UIScrollView automatically scrolling when a subview increases its width past the edge of the screen

后端 未结 3 1098
迷失自我
迷失自我 2021-02-13 13:09

In the context of the iPhone:

I have a UIScrollView containing a UIImage. When the user taps the screen inside the UIImage, a

3条回答
  •  面向向阳花
    2021-02-13 13:36

    Instead of changing the content offset, change the scroll view's display frame.

    - (void)keyboardWill:(NSNotification*)notification
    {
        CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
        // Update the scroll view's frame to the region not covered by the keyboard.
        self.frame = CGRectMake(self.fullSizedFrame.origin.x, 
                                self.fullSizedFrame.origin.y, 
                                self.fullSizedFrame.size.width, 
                                self.fullSizedFrame.size.height - keyboardSize.height);
    }
    
    - (void)keyboardWillHide:(NSNotification*)notification
    {
        // Set the frame back to the original frame before the keyboard was shown.
        self.frame = self.fullSizedFrame;
    }
    

    If you don't allow the user to change screen orientation, then fullSizedFrame could be set to the original frame of the view when it is first displayed. If changes in orientation are allowed, you'll need to calculate the appropriate value for fullSizedFrame based on the orientation.

提交回复
热议问题