需求如下:
本人最近在做毕业设计,音乐app,现在遇到问题,想做一个自适应宽度的tableviewcell,里面有三个imageView,imageView的宽度根据屏幕大小自适应,实现imageView的三等分,即imageView宽度自适应,但是imageView的高度要跟宽度是一样高的,即宽高比为1:1。(注明:我的测试图片为宽高比为2:1)。
程序运行如图
确实是实现了我的需求,但是运行的时候控制台会出现很多很多的说我的约束出现错误。
先上一下我设置的约束,这是我的xib图片
好了,现在一个一个看约束,第一个imageView的约束
第二个imageView的约束
第三个imageView的约束
我大概知道最关键的一部分错在哪里,就是我给每个imageView都加了一个约束就是 aspect ratio = 1:1,如果去掉这个约束的话程序运行时控制台是不会说约束错误的,但是去掉了这个约束的话又不是我想要的结果。
下面是控制台的输出
Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<0x7faa62826ba0 uiimageview:0x7faa62826d80.width="=" uiimageview:0x7faa62826d80.height>",0x7faa62826ba0>
"<0x7faa62827510 uiimageview:0x7faa60734960.width="=" uiimageview:0x7faa628267c0.width>",0x7faa62827510>
"<0x7faa62827560 uiimageview:0x7faa60734960.leading="=" uitableviewcellcontentview:0x7faa607347f0.leadingmargin>",0x7faa62827560>
"<0x7faa62827650 uiimageview:0x7faa628267c0.width="=" uiimageview:0x7faa62826d80.width>",0x7faa62827650>
"<0x7faa628276a0 h:>",0x7faa628276a0>
"<0x7faa62827790 uiimageview:0x7faa62826d80.top="=" uitableviewcellcontentview:0x7faa607347f0.topmargin>",0x7faa62827790>
"<0x7faa628277e0 h:>",0x7faa628277e0>
"<0x7faa62827880 uitableviewcellcontentview:0x7faa607347f0.trailingmargin="=" uiimageview:0x7faa62826d80.trailing>",0x7faa62827880>
"<0x7faa62827830 uitableviewcellcontentview:0x7faa607347f0.bottommargin="=" uiimageview:0x7faa62826d80.bottom>",0x7faa62827830>
"<0x7faa6282b8f0 v:>",0x7faa6282b8f0>
"<0x7faa6282ca60 h:>"0x7faa6282ca60>
)
Will attempt to recover by breaking constraint
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in may also be helpful.