2016-09-28 40 views
0

在我的应用程序中不使用自动布局和大小类。以编程方式创建所有元素。ios:如何设置所有iphone屏幕的动态高度

计算每个元素的视图宽度和设置框架。在这里我的代码。

UILabel* TitleLbl = [[UILabel alloc] initWithFrame:CGRectMake((self.view.frame.size.width/2)-25,y,100, 50)]; 
    [email protected]"Login"; 
    TitleLbl.textColor=[UIColor whiteColor]; 
    TitleLbl.font = [UIFont boldSystemFontOfSize:14.0]; 
    [self.view addSubview:TitleLbl]; 
    y=y+30; 

    UILabel* AccountLbl = [[UILabel alloc] initWithFrame:CGRectMake((self.view.frame.size.width/2)-130,y,350, 50)]; 
    [email protected]"Login or Create Your New Account"; 
    AccountLbl.textColor=[UIColor whiteColor]; 
    AccountLbl.font = [UIFont boldSystemFontOfSize:16.0]; 
    [self.view addSubview:AccountLbl]; 
    y=y+40; 



UIButton *LoginBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 
// [LoginBtn addTarget:self action:@selector(aMethod:) 
//  forControlEvents:UIControlEventTouchUpInside]; 
    [LoginBtn setTitle:@"Login" forState:UIControlStateNormal]; 
    LoginBtn.frame = CGRectMake(self.view.frame.origin.x+40,y, self.view.frame.size.width-80, (self.view.frame.size.height/10)); 
    LoginBtn.layer.cornerRadius = 10; 
    LoginBtn.layer.borderWidth=1.0f; 
    LoginBtn.layer.borderColor = [UIColor whiteColor].CGColor; 
    LoginBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter; 
    LoginBtn.titleLabel.font = [UIFont systemFontOfSize:16.0]; 
    //LoginBtn.backgroundColor=[UIColor orangeColor]; 
    [self.view addSubview:LoginBtn]; 


    UIButton *RegBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 
    // [RegBtn addTarget:self action:@selector(aMethod:) 
    //  forControlEvents:UIControlEventTouchUpInside]; 
    [RegBtn setTitle:@"Register Now" forState:UIControlStateNormal]; 
    RegBtn.frame = CGRectMake(self.view.frame.origin.x+40, y, self.view.frame.size.width-80, (self.view.frame.size.height/10)); 
    RegBtn.layer.cornerRadius = 10; 
    RegBtn.layer.borderWidth=1.0f; 
    RegBtn.layer.borderColor = [UIColor whiteColor].CGColor; 
    RegBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter; 
    RegBtn.titleLabel.font = [UIFont systemFontOfSize:16.0]; 
    [self.view addSubview:RegBtn]; 

我该如何为此代码计算动态heigth。 特别是 UIButton * RegBtn,UIButton * LoginBtn这两个按钮我不能够把动态高度。

任何人都可以帮助我。如何计算动态高度。为我提供任何解决方案。感谢提前。

回答

1

使用[UIScreen mainScreen].bounds来查找屏幕大小,然后相应地动态更改UI元素的宽度和高度。例如,您可能希望RegBtn比屏幕宽度小40个像素。要做到这一点,你可以简单地计算它的宽度为[UIScreen mainScreen].bounds.width – 40.0

+0

如何添加高度。 –

+0

@saravanaa'[UIscreen mainScreen] .bounds.height' –

0

constant.h类或已经为代码可重用的目的。收藏此

#define SCREEN_SIZE ([[UIScreen mainScreen] bounds].size) 

然后SCREEN_SIZE.height & SCREEN_SIZE.width任何地方在项目中使用任何屏幕尺寸无关。 (需要在任何VC中需要导入constant.h)。您可以通过从SCREEN_SIZE(如x,y坐标或高度,宽度)计算来动态设置任何控件的框架。

我刚试过你的代码,发现注册和登录按钮框架是相同的,所以它们重叠。我有固定的,它可以帮助你

UIButton *LoginBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 
// [LoginBtn addTarget:self action:@selector(aMethod:) 
//  forControlEvents:UIControlEventTouchUpInside]; 
[LoginBtn setTitle:@"Login" forState:UIControlStateNormal]; 
LoginBtn.frame = CGRectMake(self.view.frame.origin.x+40,y, SCREEN_SIZE.width-80, (SCREEN_SIZE.height/10)); 
LoginBtn.layer.cornerRadius = 10; 
LoginBtn.layer.borderWidth=1.0f; 
LoginBtn.layer.borderColor = [UIColor whiteColor].CGColor; 
LoginBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter; 
LoginBtn.titleLabel.font = [UIFont systemFontOfSize:16.0]; 
//LoginBtn.backgroundColor=[UIColor orangeColor]; 
[self.view addSubview:LoginBtn]; 

y = CGRectGetHeight(LoginBtn.frame) + y + 16; // Need to update the y position 

UIButton *RegBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 
// [RegBtn addTarget:self action:@selector(aMethod:) 
//  forControlEvents:UIControlEventTouchUpInside]; 
[RegBtn setTitle:@"Register Now" forState:UIControlStateNormal]; 
RegBtn.frame = CGRectMake(self.view.frame.origin.x+40, y, SCREEN_SIZE.width-80, (SCREEN_SIZE.height/10)); 
RegBtn.layer.cornerRadius = 10; 
RegBtn.layer.borderWidth=1.0f; 
RegBtn.layer.borderColor = [UIColor whiteColor].CGColor; 
RegBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter; 
RegBtn.titleLabel.font = [UIFont systemFontOfSize:16.0]; 
[self.view addSubview:RegBtn]; 

enter image description here

编码愉快..

0

我不使用自动布局和编程创建的一切,我一般使用这样的事情在的viewController :

@interface mainVC(){ 
float w; 
float h; 
} 
-(void)viewDidLoad { 
w = self.view.frame.size.width; 
h = self.view.frame.size.height; 
} 
-(void)layoutSomeElements { 

float yOff = 0; 

UIView * someView = [UIView new]; 
someView.frame = CGrectMake(0,yOff,w,30); 
[self.view addSubview:someView]; 

yOff += 30.0f;  

UIView * anotherView = [UIView new]; 
anotherView.frame = CGRectMake(0,yOff,w,50); 
[self.view addSubview:anotherView]; 

yOff += 50.0f; 

UIView * bottomView = [UIView new]; 
bottomView.frame = CGRectMake(0,h-50,w,50); 
[self.view bottomView]; 
} 

它很整齐,可以让你重新整理你的观点。您可以使用缩放比例,但更好的UI可以让您的元素在各种设备上始终保持不变,并使用scrollView等进行调整。