简体   繁体   English

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

[英]How to change View size programmatically?

There are few questions regarding this topic in stackoverflow. stackoverflow中关于此主题的问题很少。 But none of the solutions are working for me. 但是没有一个解决方案适合我。

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 . 此弹出窗口应该包含一个tableView Since Apple recommandation is not use tableView inside a alertView,So, I need to use normal UIView with lesser size. 由于Apple推荐不在alertView中使用tableView ,所以,我需要使用较小尺寸的普通UIView

UIView is 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. 但它没有用。

CGRect is not only width and height, its also x and y. CGRect不仅是宽度和高度,还有x和y。 So do this: 这样做:

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. 这只会影响宽度和高度。

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

Since you are requirement is to display a popup , I would suggest you if its iPAD then go ahead with UIPopoverController and if its iPhone then 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并更改框架,然后将subView附加或添加到主视图

This will solve your issue. 这将解决您的问题。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM