I have an ViewController with a View called MasterView(it's the first UIView of the ViewController)
Inside the MasterView, I have a UIImageView, both views have a width of 320, take a look on the images below:
But when I try to get the image size programmatically, I use the following code:
/* imagemPrincipalCabeca is an IBOutlet connected to the UIImageView */
print("master uiview frame: \(view.frame.width)")
print("master uiview bounds: \(view.bounds.width)")
print("subview uiimageview frame: \(imagemPrincipalCabeca.frame.width)")
print("subview uiimageview bounds: \(imagemPrincipalCabeca.bounds.width)")
I got the following text on the console:
master uiview frame: 375.0
master uiview bounds: 375.0
subview uiimageview frame: 320.0
subview uiimageview bounds: 320.0
If the UIView width and the UIImageView on my code is 320. Why the view.frame.width results in 375 ?
I know the device screen in pixels is 375 and device points in screen is 320, but why I got different values when I use the code above?
Based on the #SWeeper comment.
I test the viewDidAppear method and viewDidLayoutSubviews method.
The viewDidAppear method didn't work for me,
but the viewDidLayoutSubviews method WORKED for me.
I use a ContainerView, maybe this is the reason why I need to use the viewDidLayoutSubviews method to make my code get the correct width of my view.
Related
I have a UIView ("myUIView") in my ViewController ("mainView") that has a UILabel ("myUILabel") inside, centered vertically with autolayout constraints. I'm animating this UIView to change its height.
Originally, I thought auto layout would keep track of the animation/change and keep the UILabel centered, but instead the UILabel acts as though it is constrained to the top of the UIView.
The UILabel isn't in the animation code itself. Code for reference:
UIView.animateWithDuration(currentAnimationTime, delay: 0.0, options: .CurveEaseOut, animations: {
self.myUIView.frame = CGRectMake(0, self.mainView.frame.height * 0.2, self.mainView.frame.width, self.mainView.frame.height * 0.6 )
}, completion: nil)
How can I increase the UIView's height and keep my UILabel entered vertically? Will I need to add something that directly affects the UILabel itself, or am I going about the resize itself (CGRectMake) all wrong?
I have had luck by calling layoutIfNeeded on the animated view inside the animation.
It would be preferable to create an outlet for the height constraint of the view and then set the constant of that constraint rather than constructing a CGRect. Change the constant outside the animation as demonstrated in this answer.
I have a UITableViewController with a UIView before the table view. The UIView (IBOutlet UIView *templateDescriptionView) has a UITextView (IBOutlet UITextView *templateDescription) in it. It looks like this:
The UITextView can have a variable amount of text. I'm trying to programmatically resize the UITextView and UIView to compensate for this, but it's not working as expected.
-(void) viewDidLoad
{
[templateDescription sizeToFit];
CGRect frame = templateDescription.frame;
frame.size.height = templateDescription.contentSize.height;
templateDescription.frame = frame;
CGRect viewFrame = templateDescriptionView.frame;
viewFrame.size.height = templateDescription.contentSize.height + 40; // 40 for padding
templateDescriptionView.frame = viewFrame;
}
The problem is, when everything renders, the UIView is partially on top of the UITableView. It resizes, but it's not resizing to the proper height. What am I doing wrong? This is Xcode 5, iOS7.
I am afraid I misunderstood your meaning. But UITableViewController is used to control a UITableView. Does it run well that a UITableViewController control a UIView? Maybe you put a UIView into the HeaderInSection of tableView. In iOS7, self.automaticallyAdjustsScrollViewInsets = NO; can be used to remove the space inset in scrollview.
I have a view controller with a table view. The UITableViewCell width is set to 320 in IB. Now, I want to use the view controller in iPad by adding it to a superview. The superview's width is bigger than 320. The table view shows on the superview. The problem is that it only occupy 320. How can I change the cell width to occupy whole superview's width? Thanks.
Hey you need to create a custom UITableViewCell. Than you can overwrite the function
- (void) layoutSubviews
{
[dateLabel setFrame:CGRectMake(10.f, 16.f, 80.f, 12.f)];
[textLabel setFrame:CGRectMake(106.f, 16.f, self.frame.size.width-105.f + 1.f, 12.f)];
}
In that function the self.frame.size.width is the actual one.
And it works with rotation of the device, too.
I have a CALayer (CAGradientLayer) added as a subview of a UIImageView. I have successfully added it as a sublayer of the UIImageView, however on orientation I'd like to adjust the layer's width. How can I do so? I tried the following code without any success:
- (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[self.imageOverlay setFrame:self.backgroundImageView_.bounds];
[self.gradientOverlay setFrame:self.view.bounds];
}
Are you supporting < iOS 5? You can override the viewWillLayoutSubviews method on your view controller and try setting the frame there. Otherwise, create a view subclass to use as the container of the gradient layer and resize the layer in layoutSubviews.
I have a UIImageView (imageView) being the subView of the UIScrollView (scrollView). The scrollView is set only for pinching the showed image from imageView. And I have a small UIImageView (iconView) which is the subView of imageView. So the relationship is like:
UIView
|_____ UIScrollView ( scrollView )
|_____ UIImageView (imageView)
|______ UIImageView(iconView) .
I need to touch and move the iconView but I am confused with this two methods I found.
Can I simply use the "hitTest:withEvent:" method to work this out and how to use hitTest ?
Or Should I override "UITouchesBegan:withEvent:" "UITouchesMoved:withEvent:" "UITouchesEnd:withEvent:" ? and how to use it?
You should use the 2nd method.
Subclass the UIView class and create CustomView. Define iconView as CustomView.
In CustomView define the 3 delegates to the desired behaviour:
UITouchesBegan:withEvent:
UITouchesMoved:withEvent:
UITouchesEnd:withEvent:
Hope this helps