9

我想重置 UIView 的帧大小,所以我写道:

view.frame.size.width = x;

但它不起作用,谁能告诉我为什么?

4

4 回答 4

18

你不能直接设置宽度,这样做

CGRect frm = view.frame;
frm.size.width = x;
view.frame = frm;
于 2012-05-22T08:49:59.547 回答
7

当您调用时,view.frame您会获得 frame rect 属性的副本,因此设置它会frame.size.width更改副本的宽度而不是视图的框架大小

于 2012-05-22T08:54:46.957 回答
3

这是为什么。您的代码转换为

[view frame] // <- This hands you a CGRect struct, which is not an object.
             //    At this point, view is out of the game.
.size        // <- On the struct you were handed
.width       //             "
= x;         // <- the struct you were handed changed, but view was untouched

另一种思考方式是那里有一个不可见的变量,您无法访问:

 CGRect _ = [view frame]; // hands you a struct
 _.size.width = x;
于 2014-05-27T23:15:25.320 回答
1
-(void)changeWidth:(UIView*)view wid:(int)newWid{
    CGRect rc=view.frame;
    view.frame=CGRectMake(rc.origin.x, rc.origin.y, newWid, rc.size.height);
}

- (void) adjustViewtForNewOrientation: (UIInterfaceOrientation) orientation {
    //if (UIInterfaceOrientationIsLandscape(orientation)) {
    loadMoreView.frame=CGRectMake(0, 0, WIDTH, 50);
    headerView.frame=CGRectMake(0, [MLTool getPaddingHeight:self], WIDTH, HEI_SEGMENT);
    [self changeWidth:webview wid:WIDTH];
    [self changeWidth:tableView wid:WIDTH];
    [self changeWidth:  segmentedControl  wid:WIDTH-SEG_LEFT*2];
}
于 2015-05-30T03:58:05.160 回答