开发者

Any way to get the frame of a UIObject from a different orientation?

开发者 https://www.devze.com 2023-04-09 21:26 出处:网络
I have a piano UIView, made of Key subviews. The keys are assigned their frames relative to the frame of the piano. This works fine if I stay in portrait mode.

I have a piano UIView, made of Key subviews. The keys are assigned their frames relative to the frame of the piano. This works fine if I stay in portrait mode.

But if I start in landscape mode, the frame of the piano is landscape, but the keys set themselves up relative to the portrait mode frame of the piano. (There is a gray area, so I know the frame of the piano is different than the sum of the keys)

Is there any way to get the frame of the landscape mode piano while I'm in portrait mode? That way, I can check to see if I'm in landscape mode, and assign the appropriate frame.

The code below is in the piano class

- (void) setWhiteKeys {

CGRect baseFrame = CGRectMake(0, 0, self.frame.size.width / 7, self.frame.size.height)                
开发者_高级运维
for (Keys *key in whiteKeys) {

 // sets the key's x origin to where the previous key ended

key.frame = baseFrame;
baseFrame.origin.x = key.frame.origin.x + key.frame.size.width;
}


You need to use the method layoutSubviews. It will be called automatically at the start, and after when the device orientation changes.

In that method, have your view check it's bounds and layout any subviews accordingly, e.g.

- (void)layoutSubviews {
    CGRect bounds = self.bounds;

    // Layout your subviews here according to the bounds

The key thing to think about in this is, how do you wish your views to behave in the two different orientations? Do you want them to be bigger, or in a different place, or remain the same?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号