2

I have a subview and I have been customizing its size by using the frame property and setting its value to the CGRectMake function's parameter values.

I have slowly but surely been changing the CGRectMake parameters and re-running the app to get the subview to the correct position on the screen but I know there has to be an easier way.

Here is what I am currently doing:

UIImageView *halfView = [[UIImageView alloc]initWithImage:image];

[self.view addSubview:halfView];

halfView.frame = CGRectMake(0, 0, 320, 270);

Is there a way that I can stop having to manually enter those 4 parameters into CGRectMake, and just set it to the top 50% of the screen?

Here is what I want the subview to look like on the iphone's screen:

enter image description here

Cœur
  • 37,241
  • 25
  • 195
  • 267
user3117509
  • 833
  • 3
  • 14
  • 32

1 Answers1

7
halfView.frame = CGRectMake(0, 0, 320, self.view.bounds.size.height/2);

you just take the height and divide it by two

You should also probably change 320 to self.view.bounds.size.width

I suggest reading this post to really get a grasp of UIViews and working with them: UIView frame, bounds and center

Community
  • 1
  • 1
John Riselvato
  • 12,854
  • 5
  • 62
  • 89
  • John, you are still working with the value that I manually entered as the height. I could enter in 100 for the height and then it will still look terrible. I want to eliminate the guessing that comes with me staring at the screen trying to make sure it's perfect, and just setting it to half of the screen's size. Does that make sense? – user3117509 Feb 18 '14 at 04:40
  • 2
    @user3117509 : "_eliminate the guessing_" and hence... `self.view.bounds.size.height/2` = 50% of the view size – staticVoidMan Feb 18 '14 at 04:48
  • 1
    John, thanks for the help. Once you added the replacement for 320 I went and tried it and it looks great. I'm definitely going to spend some time learning with those resources you linked me to. Thanks. – user3117509 Feb 18 '14 at 04:50