以编程方式添加视图时,addSubview
和insertSubView
方法之间的区别是什么?
我在xib中创建了一个视图(带有活动指示器,进度视图和标签).然后我创建了.h/.m文件:
#import <UIKit/UIKit.h>
@interface MyCustomView : UIView {
IBOutlet UIActivityIndicatorView *actIndicator;
IBOutlet UIProgressView *progressBar;
IBOutlet UILabel *statusMsg;
}
@end
#import "MyCustomView.h"
@implementation MyCustomView
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
// Initialization code
}
return self;
}
- (void)dealloc {
[super dealloc];
}
@end
Run Code Online (Sandbox Code Playgroud)
在IB中,我将文件的所有者和查看标识设置为MyCustomView并将IBOutlet连接到文件的所有者
在MyViewController.m中,我:
- (void)viewDidLoad {
[super viewDidLoad];
UIView *subView = [[MyCustomView alloc] initWithFrame:myTableView.frame];
[subView setBackgroundColor:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5]];
[myTableView addSubview:subView];
[subView release];
}
Run Code Online (Sandbox Code Playgroud)
当我运行应用程序时,视图已添加,但我看不到标签,进度条和活动指示器.
我究竟做错了什么?
我是UIImageView
在Interface Bulder的帮助下创建的.现在我想在其中放置标签(作为其子视图).在代码中,我可以键入类似的内容:[myUIImageView addSubview:myUILabel];
但是我可以在IB的帮助下完成吗?我找到了解决方案UIView
,但找不到类似的东西UIImageView
.
我正在尝试将子视图添加到UIButton.这个工作正常.但是,只要我添加子视图,该按钮就不再可以点击了.
我使用以下代码:
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(column*100+24, row*80+10, 64, 64);
[button addSubview:asyncImage];
[button addSubview:price];
[button addTarget:self
action:@selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
Run Code Online (Sandbox Code Playgroud)
如果我删除了2个addsubviews,那么该按钮将再次起作用.如果有人知道如何解决这个问题那就太棒了!
日Thnx !!!
大家.
我有一些标签,我在xib文件中绘制它们,并使用代码添加背景视图,但背景视图位于这些标签的前面,所以我无法看到它们.所以,我的问题是如何在这些标签下面添加背景视图.
先感谢您.
我有一个UINavigationController.在右上方我有一个按钮点击,我必须得到一个下拉表视图.我创建了另一个UIViewController类,使用xib并将其作为子视图添加到当前视图中.它应该出现在第一次点击并在第二次点击时消失.所有点击都会发生这种情况(打开视图和关闭视图).我写了这段代码,但不知道我哪里出错了.有人请帮忙
-(void)modalTableView
{
tableView1 = [[TableViewController alloc] initWithNibName:@"TableViewController" bundle:nil];
for (UIView *subView in self.view.subviews)
{
if ([subView isKindOfClass:[TableViewController class]])
{
[subView removeFromSuperview];
}
else
{
[self.view addSubview:tableView1.view];
}
}
}
Run Code Online (Sandbox Code Playgroud)
我在这里失踪了什么?
编辑:TableViewController是我的UIViewController类的名称
我想添加一个tableViewController作为containerViewController的子视图控制器(如下所示).根据Apple的View Controller Programming Guide,我可以通过我的containerViewController中的以下代码行实现这一点:
[self addChildViewController:tableViewController];
[self.view addSubview:tableViewController.view];
[tableViewController didMoveToParentViewController:self];
Run Code Online (Sandbox Code Playgroud)
事实上,这很好.现在的问题是我不想将tableViewController的视图添加为containerViewController的根视图的子视图.相反,我想将它添加为containerView的子视图(参见图像),它本身是containerViewController的根视图的子视图.所以我改变了上面的代码如下:
[self addChildViewController:tableViewController];
[self.contentView addSubview:tableViewController.view];
[tableViewController didMoveToParentViewController:self];
Run Code Online (Sandbox Code Playgroud)
现在,当我启动应用程序时,它立即崩溃并出现此错误:
由于未捕获的异常'UIViewControllerHierarchyInconsistency'而终止应用程序,原因:'子视图控制器:应该有父视图控制器:但实际的父级是:'
这里有什么问题?是不是可以将childViewController的视图添加到其containerViewController 的特定子视图中?或者如何在视图控制器层次结构中没有错误的情况下实现此目的?
parentviewcontroller addsubview ios childviewcontroller container-view
我想添加一个带动画的子视图.我正在使用添加子视图,所以它没有显示任何动画所以我想在我这样做时显示任何动画...我使用下面的代码: -
UIViewController *vControllerHome = [[viewTemp alloc] initWithNibName:@"viewTemp" bundle:nil];
vControllerHome.view.frame =CGRectMake(0, 0, 320, 414);
[self.view addSubview:vControllerHome.view];
self.selectedViewController = vControllerHome;
Run Code Online (Sandbox Code Playgroud)
任何人都可以建议我这样做吗?
animation addsubview uiviewanimation uiviewanimationtransition
为什么我的自定义UIView
不在UIView
其父容器的容器内呈现?所以我拥有的是:
UIView
" customViewContainer
"(它不占用整个屏幕),和UIView
带有XIB文件的 - 它在UIView
这里呈现时(使用AspectFit)呈现在父" customViewContainer
" 的范围之外用于设置自定义视图的代码是MainViewController的提取:
"切 - 见下面的Update2"
所以我不明白为什么CustomView
视图以比父母更大的面积呈现customViewContainer
?我想要的是完全按照类型方法customview
适合父母" customViewContainer
" AspectFit
.
谢谢
编辑1(添加说明) - 如果我在父视图中"剪辑子视图"然后它会剪切内容,但我真正需要在父视图区域内渲染自定义视图(而不是整个屏幕区域).所以我需要(a)自定义视图的中心位于父视图的中心,以及(b)正确地将AspectFit视图放入父视图中.有任何想法吗?
编辑2 - 更新
抱歉 - 在原始问题中使用代码复制/粘贴错误 - 似乎无法编辑它所以我将在下面放置正确的版本 - 所以澄清:
MainViewController
- 有一个UIView
" containerView
"(它不占用整个屏幕),并且CustomView
- 是一个UIView
带有XIB文件 - 它在UIView
这里被渲染(带AspectFit
)渲染超出了父" containerView
" 的界限
使用下面的代码,这现在有意义吗?这个代码的原因是我有一个自定义UIView
,但我有一个与之关联的XIB文件,所以这是让我的MainController
视图能够使用它的唯一方法.也就是说,在容器视图中 …
任何人都可以告诉我如何直接设置Image UIView
?这是我的代码:
UIView *view=[[UIView alloc]init];
[view setImage:[UIImage imageNamed:@"image.png"]];
Run Code Online (Sandbox Code Playgroud) addsubview ×10
ios ×6
iphone ×4
subview ×3
objective-c ×2
uiimageview ×2
uiview ×2
animation ×1
uibutton ×1
uikit ×1
uitableview ×1
xib ×1