gpt4 book ai didi

ios - UIScrollView 在横向模式下不滚动 - 附上测试代码和屏幕截图

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:22:14 25 4
gpt4 key购买 nike

在 Xcode 5.0.2 中我创建了 a universal app并将 UIScrollViewUIImageView(其“View Mode”更改为“Top Left”)拖到 iPhone 和 iPad Storyboard中(此处为 fullscreen ):

xcode screenshot

为了让 UIScrollView 填满整个屏幕,我添加了 4 个约束(此处为 fullscreen):

xcode screenshot

然后在ViewController.m我正在尝试:

  1. 载入 board.png(版权维基百科,Denelson83)
  2. UIImageView 提供与 board.png 相同的尺寸>
  3. UIScrollViewcontentSize 设置为相同的大小

这里是源代码:

- (void)viewDidLoad
{
[super viewDidLoad];

UIImage *img = [UIImage imageNamed:@"board"];
NSLog(@"%s: (%f x %f)", __PRETTY_FUNCTION__, img.size.width, img.size.height);
_imageView.image = img;
_imageView.frame = CGRectMake(0, 0, img.size.width, img.size.height);
}

- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];

_scrollView.contentSize = _imageView.bounds.size;
}

- (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];

// fruitless attempt to solve the rotation problem
_scrollView.contentSize = _imageView.bounds.size;
}

这里是输出:

Scroll[3464:70b] -[ViewController viewDidLoad]: (1000.000000 x 961.500000)

这种方法可行,但有两个问题:

问题 1:

当我旋转 iPhone 或 iPad 模拟器时(通过按 Cmd - 向左箭头)- 图像滚动停止工作:

iphone 3.5 screenshot

ipad screenshot

问题 2:

在纵向模式下滚动有效,但我无法到达右下角:

iphone 4 screenshot

更新 1:

我已经尝试了 iphonic 的建议(谢谢!),我看到正在调用 deviceOrientationDidChange 选择器,但应用程序仍然没有滚动:

Scroll[702:70b] -[ViewController viewDidLoad]: image size = {1000, 961.5}
Scroll[702:70b] -[ViewController deviceOrientationDidChange:]: view size = {1000, 961.5}
Scroll[702:70b] -[ViewController deviceOrientationDidChange:]: view size = {1000, 961.5}
Scroll[702:70b] -[ViewController deviceOrientationDidChange:]: view size = {1000, 961.5}

(不过我使用的是 _imageView.image.size,因为 _imageView.size 在我的 sotryboard 中被定义为 100 x 100):

- (void)viewDidLoad
{
[super viewDidLoad];

UIImage *img = [UIImage imageNamed:@"board"];
NSLog(@"%s: image size = %@", __PRETTY_FUNCTION__, NSStringFromCGSize(img.size));
_imageView.image = img;
_imageView.frame = CGRectMake(0, 0, img.size.width, img.size.height);

[[NSNotificationCenter defaultCenter]
addObserver:self
selector: @selector(deviceOrientationDidChange:)
name: UIDeviceOrientationDidChangeNotification
object: nil];
}

-(void)deviceOrientationDidChange:(NSNotification *)notification

{
_scrollView.contentSize = _imageView.image.size;
NSLog(@"%s: view size = %@", __PRETTY_FUNCTION__, NSStringFromCGSize(_imageView.image.size));
}

最佳答案

  1. 删除 Orientation 和 viewDidLayoutSubviews 的代码
  2. 在 viewDidLoad 方法中提及 UIScrollView contentSize
  3. 取消选中 XIB 中绘图部分下的 UIScrollView = NO 的 AutoResizeSubview,并选中 Clip SubViews。

引用https://github.com/jayaprada-behera/ScrollViewOrientation

- (void)viewDidLoad
{
[super viewDidLoad];
//set uiscrollview.autoresizeSubview = NO; in xib
UIImage *img = [UIImage imageNamed:@"board.png"];
imageView_.image = img;

imageView_.frame = CGRectMake(imageScrollView.frame.origin.x, imageScrollView.frame.origin.y, img.size.width, img.size.height);
imageScrollView.contentSize = imageView_.bounds.size;
}

它会起作用。

关于ios - UIScrollView 在横向模式下不滚动 - 附上测试代码和屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21879154/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com