ios 如何以编程方式更改视图大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23536325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:06:01  来源:igfitidea点击:

How to change View size programmatically?

iosuitableviewuiview

提问by user1743514

There are few questions regarding this topic in stackoverflow. But none of the solutions are working for me.

stackoverflow 中关于这个主题的问题很少。但是没有一个解决方案对我有用。

I have a requirement to show pop up when user clicks on the row of a table view. Again this pop-up should contain a tableView. Since Apple recommandation is not use tableViewinside a alertView,So, I need to use normal UIViewwith lesser size.

当用户单击表视图的行时,我需要显示弹出窗口。同样,此弹出窗口应包含一个tableView. 由于 Apple 建议不在tableViewalertView 内使用,因此,我需要使用UIView较小尺寸的normal 。

UIViewis drawn using storyboard.

UIView是使用 绘制的storyboard

Programmatically I am trying following code to reduce the size.

以编程方式我正在尝试以下代码来减小大小。

- (void)viewDidLoad
{
    [super viewDidLoad];

    CGRect newFrame = self.view.frame;

    newFrame.size.width = 200;

    newFrame.size.height = 200;

    [self.view setFrame:newFrame];
}

But it is not working.

但它不起作用。

回答by Ben

CGRect is not only width and height, its also x and y. So do this:

CGRect 不仅是宽度和高度,还有 x 和 y。所以这样做:

CGRect newFrame = CGRectMake( self.view.frame.origin.x, self.view.frame.origin.y, 200, 200);

self.view.frame = newFrame;

This does only effect the width and height.

这只会影响宽度和高度。

回答by IamAnil

Since you are requirement is to display a popup , I would suggest you if its iPADthen go ahead with UIPopoverController and if its iPhonethen just create a new full screen UIView with the transparent background (clear background) and then create one more UIView with new frames and make sure the frame size is in middle and attach it(Add subview) .

由于您的要求是显示一个弹出窗口,我建议您如果它的iPAD然后继续使用 UIPopoverController,如果它的iPhone则只需创建一个新的具有透明背景(清晰背景)的全屏 UIView,然后再创建一个新的 UIView框架并确保框架大小在中间并附加它(添加子视图)。

Also the procedure what you is doing won't work.Create a new UIView and change the frame and then attach or add subView to your main View

此外,您正在执行的过程将不起作用。创建一个新的 UIView 并更改框架,然后将子视图附加或添加到您的主视图

This will solve your issue.

这将解决您的问题。

回答by apascual

Probably the View that you are trying to modify is the one attached to the UIViewControllervia Storyboard, you will not be able to modify that one, try creating another one either with Storyboards or programmatically.

可能您尝试修改的视图是附加到UIViewController通过情节提要的视图,您将无法修改该视图,请尝试使用情节提要或以编程方式创建另一个视图。

回答by Vijay-Apple-Dev.blogspot.com

I think you are looking for UIPopOverController with tableView..

我认为您正在寻找带有 tableView 的 UIPopOverController ..

Refer this tutorial

参考本教程

http://www.raywenderlich.com/29472/ipad-for-iphone-developers-101-in-ios-6-uipopovercontroller-tutorial

http://www.raywenderlich.com/29472/ipad-for-iphone-developers-101-in-ios-6-uipopovercontroller-tutorial