Objective-C I can access self.view but not self.view.frame - objective-c

I can access and show self.view and see the frame in the log but when I try to access self.view.frame I get null. Below is the log output of
NSLog(#"Show self.view:%#",self.view);
NSLog(#"Show self.view.frame:%#",self.view.frame);
-
2010-03-28 11:08:43.373 vivmed_CD_Tab[20356:207] Show self.view:<UITableView: 0x4001600; frame = (0 0; 320 583); clipsToBounds = YES; autoresize = W+H; layer = <CALayer: 0x3b21270>>
2010-03-28 11:08:43.373 vivmed_CD_Tab[20356:207] Show self.view.frame:(null)
Can anyone explain why self.view.frame is null but self.view shows a frame? My goal is to change the frame size.
Cheers,
Grant

One problem in the code you've posted is that you're printing the frame (a CGRect) as an object, which won't work. Try:
NSLog(#"Show self.view.frame: %u", self.view.frame); // as a pointer
NSLog(#"Show self.view.frame: %#", NSStringFromCGRect(self.view.frame)); // as a string

Related

Why changing the frame of UISearchBar simply doesn't work

CGRect rectSearchLocationFrame=self.rectsearchLocation;
CGRect rectSearchCityFrame = self.rectsearchCity;
rectSearchLocationFrame.size.width += change *changeByMuch;
rectSearchCityFrame.size.width -= change * changeByMuch;
rectSearchCityFrame.origin.x +=change*changeByMuch;
self.searchLocation.frame = rectSearchLocationFrame;
self.searchCity.frame = rectSearchCityFrame;
PO1(self.searchLocation.textField);
PO1(self.searchCity.textField);
//NSAssert(false, #"Hello");
I look carefully. self.searchLocation has indeed change frame
(lldb) po self.searchLocation
<BGSearchBar: 0x158e5130; baseClass = UISearchBar; frame = (0 38; 260 44); text = ''; opaque = NO; autoresize = W+BM; gestureRecognizers = <NSArray: 0x158d1ba0>; layer = <CALayer: 0x158e5220>>
You see, the frame? The width is 260 there, not 160. It shouldn't end in the middle.
Yet, this is the screenshot
If I animate the movement, it seems that it actually change size. However, it slowly goes back. Something make it goes back to it's original size.
I got this error:
invalid context 0x0. This is a serious error. This application, or a
library it uses, is using an invalid context and is thereby
contributing to an overall degradation of system stability and
reliability. This notice is a courtesy: please fix this problem. It
will become a fatal error in an upcoming update.
try this
[UIView beginAnimations:nil context:nil];
<duration of animation>
<change frame of your search bar>
[UIView commitAnimations];
or
[UIView
animateWithDuration:0.1
animations:^{
<change frame of your search bar>
}
completion:^(BOOL finished) {
}];
-(void) setActiveSearchBar:(BGSearchBar *)activeSearchBar
{
_activeSearchBar=activeSearchBar;
AssertMainThread
if (activeSearchBar==self.searchLocation)
{
[self vChangeFrameBy:1];
}
else if (activeSearchBar==self.searchCity)
{
[self vChangeFrameBy:-1];
}
else if (activeSearchBar==self.searchBusiness)
{
[self vChangeFrameBy:0];
}
}
I made a stupid bug
Look at the code above. There is an extra ; on the original code after the last else if
So the last else if was like this:
else if (activeSearchBar==self.searchBusiness);
{
[self vChangeFrameBy:0];
}
Which mean [self vChangeFrameBy:0]; is always called no matter what :(

Getting the image from a UIButton

I'm trying to get the image assigned to a button on click, so I can assign it to something else when the button is clicked.
I think I'm nearly there, I've managed to get the following info from the button
Sender info is: <UIImageView: 0x757d9b0; frame = (0 0; 100 100); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x757da10>> - image-name.png
As you can see, the image name is appended at the end of the log but I'm not sure how to get just that.
The code I've used to get this is:
sender.imageView
Thanks in advance for any help
EDIT:
Thanks to #zoul for leading me on the right path.
I ended up doing the following:
UIImage *image = [sender imageForState:UIControlStateNormal];
[self.newButton setImage:image forState:UIControlStateNormal];
There’s an imageForState: method on UIButton.

Why the frame of the UILabel in standard UITableViewCell is (0,0,0,0) and What's the Standard Size of UITableViewCell.textLabel?

I want to replace that with a webview so I can display html.
PO(cell.subviews);
PO(cell);
PO(cell.contentView.subviews);
The webview need to have the same frame with the frame of UITableViewCell.
This is what we got:
2013-01-31 12:37:35.204 BadgerNew[3888:c07] cell.subviews: (
"<UITableViewCellContentView: 0xa057140; frame = (0 0; 320 44); layer = <CALayer: 0xa057180>>"
)
2013-01-31 12:37:35.204 BadgerNew[3888:c07] cell: <UITableViewCell: 0xa0c37a0; frame = (0 0; 320 44); text = 'atm'; layer = <CALayer: 0xa0c8530>>
2013-01-31 12:37:35.205 BadgerNew[3888:c07] cell.contentView.subviews: (
"<UILabel: 0xa0d12a0; frame = (0 0; 0 0); text = 'atm'; clipsToBounds = YES; userInteractionEnabled = NO; layer = <CALayer: 0xa0571b0>>"
)
Moreover, to further confirm
po [cell textLabel]
(UILabel *) $1 = 0x0a0d12a0 <UILabel: 0xa0d12a0; frame = (0 0; 0 0); text = 'atm'; clipsToBounds = YES; userInteractionEnabled = NO; layer = <CALayer: 0xa0571b0>>
What is the final standard frame for normal UITableViewCell then?
I also did
-(void)layoutSubviews
{
PO(self);
PO(self.textLabel);
while (false);
}
and got
2013-02-01 09:36:02.048 BadgerNew[1436:c07] self.textLabel: <UILabel: 0x8923390; frame = (0 0; 0 0); clipsToBounds = YES; userInteractionEnabled = NO; layer = <CALayer: 0x89e8fc0>>
Doing it in
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
while (false);
}
yield the same result
Then I did:
-(void)layoutSubviews
{
[super layoutSubviews];
PO(self);
PO(self.textLabel);
while (false);
}
Again, another 0 frame.
The frames of the subviews within a cell are set after -tableView:cellForRowAtIndexPath.
Make a subclass of UITableViewCell, it's really the best way of handling things like this. You can override -textLabel in your subclass and bend it to your will, or you can override -layoutSubviews and use -textLabel's frame then.
One final warning, mucking around with the internals of an object causes brittle code that could break at any revision of iOS.

Why size to fit doesn't work at all and do nothing in this obvious scenario

Let's examine this line of codes (only 1 actually change anything)
PO(self.containerForFormerHeader);
PO(self.containerForFormerHeader.subviews);
[self.containerForFormerHeader sizeToFit];
PO(self.containerForFormerHeader);
Result:
self.containerForFormerHeader: <UIView: 0x8b669c0; frame = (0 75; 320 0); autoresize = W+H; layer = <CALayer: 0x8b5eb30>>
self.containerForFormerHeader.subviews: (
"<UIImageView: 0xd572210; frame = (0 0; 320 10); autoresize = LM+RM+TM; userInteractionEnabled = NO; layer = <CALayer: 0xd572250>> - shading-top-Table.png"
)
self.containerForFormerHeader: <UIView: 0x8b669c0; frame = (0 75; 320 0); autoresize = W+H; layer = <CALayer: 0x8b5eb30>>
From the result it's obvious that:
The view has a height of 0.
The view has a subview whose height is 10
After sizetofit, nothing change. How come?
I can compute the frame directly however, this bothers me.
When you call the sizeToFit method on a view, it ends up calling the sizeThatFits: method on the view. The default implementation of sizeThatFits: returns the view's current size.
So unless your custom view explicitly overrides the sizeThatFits: method to return an appropriate size to contain its subviews, nothing will change size when you call sizeToFit.
This is all spelled out in the docs for UIView sizeToFit and UIView sizeThatFits:.
Classes like UILabel do implement sizeThatFits: to return an appropriate size.

UIView.superView (BOOL) == Fail

I have a simple method that checks for the superview of a view, and returns a BOOL. However, intermittently the method fails to produce the right answer (or at least the answer Iʻm expecting).
The method is:
-(BOOL) isTheViewShowing
{
return (BOOL) [theView superview];
}
There is a Tap Gesture that removes the view, and I logged the following (when the undesirable happens):
isTheViewShowing: NO
theView: <UIView: 0x3c1a10; frame = (0 232; 320 135); clipsToBounds = YES; layer = <CALayer: 0x365480>>,
superView: <UIView: 0x392400; frame = (0 0; 320 367); autoresize = W+H; layer = <CALayer: 0x35fe90>>
As the log shows the superView exists, but why is the cast failing?
superView: <UIView: 0x392400
Note that the low order byte of superview is 0x00. I haven't really looked into what happens when you try to cast a pointer to a BOOL, but since a BOOL is essentially a char I wouldn't be at all surprised if the compiler just looks at the low order byte. Try this instead:
-(BOOL) isTheViewShowing
{
return ([theView superview] != nil);
}
That's better on two counts: 1) it actually works, and 2) it better communicates what you're trying to do.