我想重置 UIView 的帧大小,所以我写道:
view.frame.size.width = x;
但它不起作用,谁能告诉我为什么?
我想重置 UIView 的帧大小,所以我写道:
view.frame.size.width = x;
但它不起作用,谁能告诉我为什么?
你不能直接设置宽度,这样做
CGRect frm = view.frame;
frm.size.width = x;
view.frame = frm;
当您调用时,view.frame
您会获得 frame rect 属性的副本,因此设置它会frame.size.width
更改副本的宽度而不是视图的框架大小
这是为什么。您的代码转换为
[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;
-(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];
}