简体   繁体   English

如何在iOS中更改自定义UIView的大小

[英]How to change the size of your custom UIView in iOS

I want to use custom view as a toolbar at bottom of my view controller and then design it in my own way. 我想在我的视图控制器底部使用自定义视图作为工具栏,然后以我自己的方式设计它。 I am using UIView for this purpose and then add as a subview. 我正在使用UIView用于此目的,然后添加为子视图。 Somehow, the height of my custom tool bar is not changing. 不知何故,我的自定义工具栏的高度没有变化。 Even, I turned off constraints for it but somehow, it auto adjusts. 甚至,我关闭它的约束,但不知何故,它自动调整。 How can, I do this? 我怎样才能做到这一点? Below is my code snippet. 以下是我的代码段。

//CameraActivity.h class
@interface CameraActivity : UIViewController

//Custom UIView.
@property (nonatomic, strong) UIView *toolbar;

@end

//CameraActivity.m class, in viewDidLoad.

_toolbar.translatesAutoresizingMaskIntoConstraints = YES;
    [self.view removeConstraints:self.view.constraints];
    [_toolbar removeConstraints:_toolbar.constraints];
    CGRect frame = CGRectMake(0, [[UIScreen mainScreen] bounds].size.height - 44, [[UIScreen mainScreen] bounds].size.width, 150);
    _toolbar = [[UIView alloc]initWithFrame:frame];
    [_toolbar setBackgroundColor:[UIColor colorWithRed:116.0/255.0 green:174.0/255.0 blue:220.0/255.0 alpha:0.75]];

    [self.view addSubview:_toolbar];

Before creating (ie _toolbar = [[UIView alloc]initWithFrame:frame]; ) your view toolbar , using it will cause this issue. 在创建之前(即_toolbar = [[UIView alloc]initWithFrame:frame]; )您的视图toolbar ,使用它将导致此问题。

Hence, change the line order to this : 因此,将行顺序更改为:

 CGRect frame = CGRectMake(0, [[UIScreen mainScreen] bounds].size.height - 44, [[UIScreen mainScreen] bounds].size.width, 150);
_toolbar = [[UIView alloc]initWithFrame:frame];
_toolbar.translatesAutoresizingMaskIntoConstraints = YES;
[self.view removeConstraints:self.view.constraints];
[_toolbar removeConstraints:_toolbar.constraints];
[_toolbar setBackgroundColor:[UIColor colorWithRed:116.0/255.0 green:174.0/255.0 blue:220.0/255.0 alpha:0.75]];
[self.view addSubview:_toolbar];

Also I suggest setting frame must done in -(void) viewDidLayoutSubviews beacause viewDidLoad is not a good place for this! 另外我建议设置框架必须在-(void) viewDidLayoutSubviews因为viewDidLoad不是一个好地方! so, 所以,

-(void) viewDidLayoutSubviews 
{
 _toolbar.frame  = CGRectMake(0, [[UIScreen mainScreen] bounds].size.height - 44, [[UIScreen mainScreen] bounds].size.width, 150);

}

I added UIView from objects library instead of adding it programmatically and then sets its width according to screen width of device and height to constant. 我从对象库中添加了UIView,而不是以编程方式添加它,然后根据设备的屏幕宽度和高度将其宽度设置为常量。 Also assigned some constraints solve my problem. 还分配了一些约束来解决我的问题。 需要设置约束

//CameraActivity.h class
@interface CameraActivity : UIViewController

//This time not custom UIView but IBOutlet.
@property (nonatomic, strong) IBOutlet UIView *toolbar;

@end

//CameraActivity.m class
#import "CameraActivity.h"
@implementation CameraActivity

-(void)viewDidLoad
{
    [super viewDidLoad];
    //Get your device screen width.
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    _toolbar.frame = CGRectMake(0, 44, width, 150);
}

Make an outlet of your UIView height constraint. 制作UIView高度约束的出口。 Then try to change its height based on device's type as follow:- 然后尝试根据设备的类型更改其高度,如下所示: -

In ViewController.h file, Create an outlet:- ViewController.h文件中,创建一个插座: -

@property (strong, nonatomic) IBOutlet NSLayoutConstraint *viewHeightConstraint;

In ViewController.m file, Change your height's constraint like this:- ViewController.m文件中,更改高度的约束,如下所示: -

if (Device is iPhone 4s) {
    viewHeightConstraint.constant = 30.0;  //Your desired height
}
else if (Device is iPhone 5 || Device is iPhone 5s || Device is iPhone 5c) {
   viewHeightConstraint.constant = 40.0;  //Your desired height
}
else if (Device is iPhone 6) {
   viewHeightConstraint.constant = 50.0;  //Your desired height
}
else if (Device is iPhone 6 Plus) {
   viewHeightConstraint.constant = 60.0;  //Your desired height
}

Happy Coding!! 快乐编码!!

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

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