iphone - 如何控制 subview 的大小和布局?

标签 iphone objective-c ios ipad uiviewcontroller

我正在深入研究 iOS 开发,并试图找出如何控制多个 subview (在本例中为两个 subview )的大小和布局。我在不同的上下文中发布了这个问题,但我的问题没有得到回答,所以我将其重新发布为一个更简单的问题。我试图添加到 subview 并将它们分别定位在 (0, 0) 和 (100, 100),但我添加的第一个 View (主视图)最终填满了整个屏幕。我的代码非常简单,我做错了什么阻止我控制两个 subview 的大小和位置?

MySplitViewController.m

#import "MySplitViewController.h"
#import "MasterViewController.h"
#import "DetailViewController.h"

@interface MYSplitViewController (){}

@property (nonatomic, strong) MasterViewController *masterViewController;
@property (nonatomic, strong) DetailViewController *detailViewController;

@end

@implementation MySplitViewController

- (id)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if (self) {
      self.masterViewController = [[MasterViewController alloc] initWithNibName:nil bundle:nil];
      self.detailViewController = [[DetailViewController alloc] initWithNibName:nil bundle:nil];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.frame = CGRectMake(0, 0, 768, 1004);
    [self.view addSubview:self.masterViewController.view];
    [self.masterViewController viewDidLoad];
    [self.view addSubview:self.detailViewController.view];
    [self.detailViewController viewDidLoad];
}

@end

MasterViewController.m

#import "MasterViewController.h"

@interface MasterViewController ()

@end

@implementation MasterViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    UITableView *table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 100, 1004) style:UITableViewStylePlain];
    table.dataSource = self;
    table.delegate = self;
    [self.view addSubview:table];
}

@end

DetailViewController.m

#import "DetailViewController.h"

@interface DetailViewController ()

@end

@implementation DetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    UIView *uiview = [[UIView alloc] initWithFrame:CGRectMake(100, 0, 668, 1004)];
    [self.view addSubview:uiview];
}

@end

非常感谢您的智慧!

最佳答案

在将 subview 添加到 self.view 之前,尝试为 self.masterViewController.viewself.detailViewController.view 设置框架。

例如:-

self.masterViewController.view.frame = CGRectMake(0, 0, 100, 100);
self.detailViewController.view.frame = CGRectMake(100, 100, 200, 200);

并且您不应该直接调用 viewDidLoad 方法。一旦 View 加载到屏幕上,它就会自动调用。

关于iphone - 如何控制 subview 的大小和布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12806091/

相关文章:

iphone - 为什么 UIApplication 在我的 Mac 应用程序中不起作用

iphone - MPMoviePlayer 声音在模拟器和 ipad 设备中工作,但在 iPhone 设备中不工作

iphone - 如何为 UIButton 添加操作

ios - 使用 ABAddressBookRegisterExternalChangeCallback 或任何其他通知获取已删除的联系人

ios - 在 react native 组件类中找不到方法

ios - 从 View Controller 上的方法内部测试/模拟称为类方法的 UIAlertView

objective-c - 动画 CALayer 隐藏

iphone - WWDC2008 和 2009 iPhone 视频

iphone - CATransaction CompletionBlock 立即触发

ios - "Cannot assign image to *UIImageView*"SWIFT 错误(iOS)