2016-09-28 70 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等進行調整。

相關問題