我的图像视图存在问题,我正在动态添加到我的滚动视图中。我正在使用iOS 8和大小类和自动布局。滚动视图已通过故事板添加,我还添加了约束以匹配视图的大小。
挑战在于,动态添加的图像视图从中心开始,但在方向更改后,它会在滚动视图中移位。
这里有很多解决方案,其中大部分指向覆盖 ViewWillLayoutSubviews()函数,然后将UIImageView的中心设置为滚动视图。这些建议都没有对我有用。每当我旋转设备时,imageview都会移位。
我尝试了一些建议
UIScrollView with centered UIImageView, like Photos app
Center UIImageView inside UIScrollView without contentInset?
以下是我添加UIImageView的方法。此函数在ViewDidLoad()中调用。
public void AddImageInScrollView(int index)
{
try
{
//Setting up the imageView
var imageUrl = Items.fetchImages[index];
//Setting up the imageView
imageView = new UIImageView();
//call method to load the images
imageView.SetImage(
url: new NSUrl(imageUrl.AbsoluteUri),
placeholder: UIImage.FromFile("sample.png"),
completedBlock: (image, error, type, url) =>
{
imageResizer = new ImageResizer(image);
imageResizer.RatioResize(100,100);
UIImage resizedImage = imageResizer.ModifiedImage;
//when download completes add it to the list
Car.images.Add(resizedImage);
});
//end method call
imageView.UserInteractionEnabled = true;
imageView.ContentMode = UIViewContentMode.ScaleAspectFit;
imageView.Center = ScrollViewMain.Center;
//Adding the imageView to the scrollView as subView
ScrollViewMain.AddSubview(imageView);
//ScrollViewMain.ContentSize = imageView.Image.Size;
ScrollViewMain.ContentSize = new CGSize(ScrollViewMain.Frame.Size.Width,ScrollViewMain.Frame.Size.Height);
catch (Exception ex)
{
Console.WriteLine(ex.Message + ex.StackTrace);
}
}
ViewWillLayoutSubviews功能
public override void ViewWillLayoutSubviews ()
{
base.ViewWillLayoutSubviews ();
imageView.Frame = new CGRect(0,0,ScrollViewMain.Frame.Size.Width,ScrollViewMain.Frame.Size.Height);
CenterScrollViewContents ();
}
CenterScrollViewContents功能
public void CenterScrollViewContents()
{
try
{
var boundsSize = ScrollViewMain.Bounds.Size;
var contentsFrame = imageView.Frame;
if (contentsFrame.Size.Width < boundsSize.Width) {
contentsFrame.X = ((float)boundsSize.Width - (float)contentsFrame.Size.Width) / 2.0f;
} else {
contentsFrame.X = 0.0f;
}
if (contentsFrame.Size.Height < boundsSize.Height) {
contentsFrame.Y = ((float)boundsSize.Height - (float)contentsFrame.Size.Height) / 2.0f;
} else {
contentsFrame.Y = 0.0f;
}
imageView.Frame = contentsFrame;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
旋转后的样子屏幕截图
答案 0 :(得分:1)
将代码从ViewWillLayoutSubviews
移至ViewDidLayoutSubviews
。不同之处在于ViewWillLayoutSubviews是&#34;被调用以通知视图控制器其视图即将布局其子视图。&#34; 和ViewDidLayoutSubviews是&#34;被调用以通知视图控制器其视图刚刚布置了其子视图。&#34;
问题是,ScrollViewMain.Frame
是
{X=0,Y=0,Width=375,Height=667} in ViewWillLayoutSubviews
但是
{X=0,Y=0,Width=667,Height=375} in ViewDidLayoutSubviews
该功能看起来像
public override void ViewDidLayoutSubviews()
{
base.ViewDidLayoutSubviews();
ScrollViewMain.ContentSize = new CGSize(ScrollViewMain.Frame.Size.Width, ScrollViewMain.Frame.Size.Height);
imageView.Frame = new CGRect(0, 0, ScrollViewMain.Frame.Size.Width, ScrollViewMain.Frame.Size.Height);
CenterScrollViewContents();
}
Bonus Protip:
您可以使用imageView.Center = ScrollViewMain.Center;
来查看视图。
如果您有更多时间,请查看Autolayout约束。他们为你节省了帧计算的痛苦。
答案 1 :(得分:0)
我设法使用一个简单的解决方案来解决这个问题。我意识到在初始化imageView时,我没有将帧大小传递给它的构造函数。有多种方法可以做到这一点,但对于我的情况,在屏幕旋转后,似乎大小没有更新。
在:
imageView = new UIImageView();
imageView.Frame = new CGRect(0,0,ScrollViewMain.Frame.Size.Width,ScrollViewMain.Frame.Size.Height);
解决方案:
var frame = new CGRect(0,0,ScrollViewMain.Frame.Size.Width,ScrollViewMain.Frame.Size.Height);
//Setting up the imageView
imageView = new UIImageView(frame);
即使在方向更改后,scrollView内的图像也始终居中。